开发者

Filtering rows withing Admin using a Queryset - Django

开发者 https://www.devze.com 2023-01-03 01:56 出处:网络
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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消