Filtering rows withing Admin using a Queryset - Django
I'm trying to find a way to filter down rows of objects within Django Admin, using a queryset.
e.g. Person.objects.filter(Q(n开发者_JAVA百科ame='John')|Q(surname='Doe'))
I'm finding quite complicated to figure out.
Any ideas?
You might be able to accomplish this by overriding the queryset() method on your modeladmin instance. See http://code.djangoproject.com/browser/django/trunk/django/contrib/admin/options.py?rev=15347#L196
# untested code
class MyModelAdmin(admin.ModelAdmin):
def queryset(self, request):
qs = super(MyModelAdmin, self).queryset(request)
return qs.filter(Q(name='John') | Q(surname='Doe'))
This would only affect results for the model registered with that ModelAdmin, but you could probably subclass it as a starting point for other ModelAdmin classes in order to stay DRY.
I'm not saying this is a good idea.
精彩评论