Django Admin:获取QuerySet根据GET字符串进行过滤,完全如变更列表所示? [英] Django Admin: Getting a QuerySet filtered according to GET string, exactly as seen in the change list?

查看:795
本文介绍了Django Admin:获取QuerySet根据GET字符串进行过滤,完全如变更列表所示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django管理员中,用户可以设置限制更改列表中显示的行的过滤器。如何使用查询字符串定义的过滤器获取QuerySet实例?例如,如果我通过了start_date_gte = 2009-11-06,Django管理员将在某处应用一个qs.filter(start_date__gte ...)如何访问这样的QuerySet?

In the Django admin, the user can set filters which limit the rows displayed in the change list. How can I get a QuerySet instance with filters set as defined by the query string? For instance, if I pass ?start_date_gte=2009-11-06, the Django admin will apply a qs.filter(start_date__gte...) somewhere. How can I access such a QuerySet?

我需要这个,因为显然我不想重写这些GET参数的代码,并且相应地过滤()sa QuerySet。

I need this since obviously I don't want to rewrite the code that takes these GET parameters and filter()s a QuerySet accordingly.

推荐答案

看起来很有趣。但是,我在谈论在Admin中使用该QuerySet。找到答案,见下文:

Looks interesting. However, I was talking about using that QuerySet within the Admin. Found the answer, see below:

class MyAdmin(admin.ModelAdmin):
  def changelist_view(self, request, extra_context=None):
    from django.contrib.admin.views.main import ChangeList
    cl = ChangeList(request, self.model, self.list_display, self.list_display_links, self.list_filter, self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self.list_editable, self)
    # getting query set with same filters like current change list
    filtered_query_set = cl.get_query_set()

这篇关于Django Admin:获取QuerySet根据GET字符串进行过滤,完全如变更列表所示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆