Django ModelAdmin 查询集覆盖不起作用

发布于 2024-10-14 22:22:25 字数 638 浏览 7 评论 0原文

我试图重写 ModelAdmin 类的 queryset() ,以便管理中显示的对象列表将按两个级别排序。

我已经尝试了以下代码,但它不起作用,即表未按预期排序

class ProductAdmin(admin.ModelAdmin):
    def queryset(self, request):
        qs = super(ProductAdmin, self).queryset(request)
        return qs.order_by('category','market')

    list_display = ('category', 'market', 'name', 'quantity')

admin.site.register(Product, ProductAdmin)

顺便说一句,您不能专门使用 ordering = ('category','market') 作为 django声明只有排序元组中的第一项生效(请参阅文档中的注释 此处)

I'm trying to override the queryset() of a ModelAdmin class so that the list of objects shown in admin would be sorted by two levels.

I've tried the following code, but it does not work, i.e. the table is not sorted as expected

class ProductAdmin(admin.ModelAdmin):
    def queryset(self, request):
        qs = super(ProductAdmin, self).queryset(request)
        return qs.order_by('category','market')

    list_display = ('category', 'market', 'name', 'quantity')

admin.site.register(Product, ProductAdmin)

btw, you can't use ordering = ('category','market') as django specifically states that only the first item in the ordering tuple takes effect (see note in the documentation here)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我的黑色迷你裙 2024-10-21 22:22:25

get_queryset 适用于 Django 1.8。

get_queryset works in Django 1.8.

誰認得朕 2024-10-21 22:22:25

我确实遇到了这个问题。这就是我所做的:

我对 ChangeList 进行子类化并覆盖 ChangeList.get_query_set 以重做之前由 ChangeList.get_ordering 更改的正确 order_by:

这是我在我的例子中做了什么(我正在使用 django-easytree,但这同样适用于 django-mptt):

class MyChangeList(ChangeList):

    def get_query_set(self):
        qs = super(MyChangeList, self).get_query_set()

        return qs.order_by('tree_id', 'lft')

另外,检查这些票证:

I had this exactly problem. Here's what I did:

I subclassed ChangeList and overrode ChangeList.get_query_set to redo the correct order_by that was previously changed by ChangeList.get_ordering:

This is what I did in my case (I'm using django-easytree, but the same applies for django-mptt):

class MyChangeList(ChangeList):

    def get_query_set(self):
        qs = super(MyChangeList, self).get_query_set()

        return qs.order_by('tree_id', 'lft')

Also, check these tickets:

一个人练习一个人 2024-10-21 22:22:25

Django 1.4 的发行说明假设 Django 现在支持管理界面中的多重排序

管理更改列表现在支持对多列进行排序。它
尊重排序属性的所有元素,并按
通过单击标题来显示多个列旨在模仿
桌面 GUI 的行为。

以及来自 ModelAdmin 排序 :

设置ordering以指定对象列表应如何在
Django 管理视图。这应该是相同格式的列表或元组
作为模型的 ordering 参数。 [...] Django 尊重列表/元组中的所有元素;在 1.4 之前,只尊重第一个。

半相关的说明 - 如果您确实覆盖 queryset 以提供自定义排序顺序,则 Changelist 似乎将覆盖该排序顺序。它应用 ordering 参数中找到的任何排序,如果没有,它会应用 pk 的默认排序,从而否定您在 中所做的任何排序查询集

我认为它应该有效 - 至少这个 Django Ticket 说 < em>已修复。但几天前我只是尝试使用 queryset 应用自定义排序,但它对我来说根本不起作用。即使在单个字段上排序,似乎在最终视图中也被覆盖。所以要么我做错了什么,要么事情还没有完全解决。 :)

请注意,可以通过代码进行自定义排序,但您必须子类化 Changelist,并重写其 get_query_set() 方法,根据此代码段。 (虽然这有点矫枉过正,但如果你只需要对多个字段进行排序,因为 Django 1.4 现在支持多个字段)。

The release notes for Django 1.4 say that Django now supports Multiple sort in admin interface:

The admin change list now supports sorting on multiple columns. It
respects all elements of the ordering attribute, and sorting on
multiple columns by clicking on headers is designed to mimic the
behavior of desktop GUIs.

And from ModelAdmin ordering:

Set ordering to specify how lists of objects should be ordered in the
Django admin views. This should be a list or tuple in the same format
as a model's ordering parameter. [...] Django honors all elements in the list/tuple; before 1.4, only the first was respected.

On a semi-related note - if you do override queryset to provide a custom sort order, it seems that the Changelist will override that sort order. It applies any sorting found in the ordering parameter, and if there isn't one, it applies a default sort by pk, thus negating any sorting you did in queryset.

I think it's supposed to work - at least this Django Ticket says fixed. But I was just trying to apply a custom sort using queryset a few days ago, and it didn't work at all for me. Even sorting on a single field, seemed to be overridden in the final view. So either I did something wrong, or it's not all that fixed. :)

Note that it is possible to do a custom sort via code, but you have to subclass Changelist, and override its get_query_set() method, as per this snippet. (Although this is overkill, if you only need multiple fields sorted, since Django 1.4 now supports multiple fields).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文