Django ModelAdmin 查询集覆盖不起作用
我试图重写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
get_queryset
适用于 Django 1.8。get_queryset
works in Django 1.8.我确实遇到了这个问题。这就是我所做的:
我对
ChangeList
进行子类化并覆盖ChangeList.get_query_set
以重做之前由ChangeList.get_ordering
更改的正确 order_by:这是我在我的例子中做了什么(我正在使用 django-easytree,但这同样适用于 django-mptt):
另外,检查这些票证:
"NFA:如果未指定默认排序,则不要覆盖 order_by"
"Admin ChangeList 不应用 ModelAdmin.queryset 指定的 'order_by' 子句"
I had this exactly problem. Here's what I did:
I subclassed
ChangeList
and overrodeChangeList.get_query_set
to redo the correct order_by that was previously changed byChangeList.get_ordering
:This is what I did in my case (I'm using django-easytree, but the same applies for django-mptt):
Also, check these tickets:
"NFA: Don't override order_by if no default ordering is specified"
"Admin ChangeList doesn't apply 'order_by' clause specified by ModelAdmin.queryset"
Django 1.4 的发行说明假设 Django 现在支持
管理界面中的多重排序
:以及来自 ModelAdmin 排序 :
半相关的说明 - 如果您确实覆盖
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
:And from ModelAdmin ordering:
On a semi-related note - if you do override
queryset
to provide a custom sort order, it seems that theChangelist
will override that sort order. It applies any sorting found in theordering
parameter, and if there isn't one, it applies a default sort bypk
, thus negating any sorting you did inqueryset
.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 itsget_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).