Django管理员change_list视图获取ChangeList查询集-比我的猴子补丁更好的解决方案 [英] Django admin change_list view get ChangeList queryset - better solution than my monkey patch

查看:64
本文介绍了Django管理员change_list视图获取ChangeList查询集-比我的猴子补丁更好的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Django管理中获取更改列表视图queryset.目前,我有这个猴子补丁,可以进行4个额外的查询,因此我正在寻找更好的解决方案.

我的观点是:我想将一些额外的值传递给django admin change_list.html模板,该模板是从创建查询中获得的.对于这些查询,我需要在django管理员变更列表视图中使用了应用了请求过滤器的queryset.这是我看到的已过滤,已排序等数据.我想从此数据制作图形.

你了解我吗?谢谢

 #admin.py从django.contrib.admin.views.main导入ChangeListTicketAdmin(admin.ModelAdmin)类:def changelist_view(self,request,extra_context = None):cl = ChangeList(request,自我模型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_max_show_all,self.list_editable,自我)#3个额外的查询filter_query_set = cl.get_query_set(request)#1个额外的查询currency_count = filtered_query_set.values('bookmaker__currency').distinct().count()extra_context = {'currencies_count':currency_count,}返回super(TicketAdmin,self).changelist_view(request,extra_context = extra_context) 

解决方案

我不知道这是否能回答您的问题,但类 ChangeList 具有名为 query_set (您可以在 https://github.com/django/django/blob/master/django/contrib/admin/views/main.py 中找到代码)已经包含查询集.

顺便说一句 changelist_view()函数(源于 https://github.com/django/django/blob/master/django/contrib/admin/options.py >)返回 TemplateResponse (来源为https://github.com/django/django/blob/master/django/template/response.py )具有一个名为 context_data 的变量,该变量指向上下文.您可以尝试扩展此变量的内容.

以下遵循未经测试的代码

  TicketAdmin(admin.ModelAdmin)类:def changelist_view(self,request,extra_context = None):响应=超级(TicketAdmin,自我).changelist_view(请求,extra_context)filtered_query_set = response.context_data ["cl"].querysetcurrency_count = filtered_query_set.values('bookmaker__currency').distinct().count()extra_context = {'currencies_count':currency_count,}response.context_data.update(extra_context)返回响应 

I need to get a changelist view queryset in django admin. Currently, I have this monkey patch which makes 4 extra queries, so I'm looking for a better solution.

My point is: I want to pass some extra values to django admin change_list.html template which I get from creating queries. For those queries, I need the queryset which is used in django admin changelist view with request filters applied. This is the same data which I see filtered, ordered etc. I want to make graphs from this data.

Do you understand me? Thanks

#admin.py
from django.contrib.admin.views.main import ChangeList

class TicketAdmin(admin.ModelAdmin):

    def changelist_view(self, request, extra_context=None):

        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_max_show_all, 
                        self.list_editable, 
                        self) # 3 extra queries
        filtered_query_set = cl.get_query_set(request) # 1 extra query

        currencies_count = filtered_query_set.values('bookmaker__currency').distinct().count()

        extra_context = {
            'currencies_count': currencies_count,
        }
        return super(TicketAdmin, self).changelist_view(request, extra_context=extra_context)

解决方案

I don't know if this answers to your question but the class ChangeList has an attribute called query_set (you can find the code here https://github.com/django/django/blob/master/django/contrib/admin/views/main.py) already containing the queryset.

BTW the changelist_view() function (source at https://github.com/django/django/blob/master/django/contrib/admin/options.py) returns a TemplateResponse (source at https://github.com/django/django/blob/master/django/template/response.py) that has a variable named context_data which points to the context. You can try to extend the content of this variable.

Below follows the untested code

class TicketAdmin(admin.ModelAdmin):

    def changelist_view(self, request, extra_context=None):
        response = super(TicketAdmin, self).changelist_view(request, extra_context)
        filtered_query_set = response.context_data["cl"].queryset

        currencies_count = filtered_query_set.values('bookmaker__currency').distinct().count()
        extra_context = {
             'currencies_count': currencies_count,
        }
        response.context_data.update(extra_context)

        return response

这篇关于Django管理员change_list视图获取ChangeList查询集-比我的猴子补丁更好的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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