Django admin search: how to override the default handler?

前端 未结 3 964
小鲜肉
小鲜肉 2021-02-14 08:00

I wish to customize the way in which search queries across the search_fields.

Is there a way to do it without hacking deeply into the Django code or creating a totally i

3条回答
  •  鱼传尺愫
    2021-02-14 08:38

    you can add an ModelAdmin method:

    def queryset(self, request):
        qs = super(MyModelAdmin, self).queryset(request)
        # modify queryset here, eg. only user-assigned tasks
        qs.filter(assigned__exact=request.user)
        return qs
    

    you have a request here, so most of the stuff can be view dependent, including url parameters, cookies, sessions etc.

提交回复
热议问题