views:

577

answers:

1

By default Django admin site shows all records of a related model/table for viewing. How can I show only the records that meet certain criteria?

+6  A: 

In your admin definition, you can define a queryset() method that returns the queryset for that model's admin. eg:

class MyModelAdmin(admin.ModelAdmin):
    def queryset(self, request):
        qs = super(MyModelAdmin, self).queryset(request)
        return qs.filter(user=request.user)

Then only objects with user=request.user will be visible in the admin.

Will Hardy
+1 I'm so happy to see this and it turned out to be so simple! Thanks!
Viet