Django 管理页面:查询集额外字段可以用于对特定列进行排序
因此,查询集中的额外字段可用于向选择查询添加其他列,而这些列又可以设置为默认排序。到目前为止,我已经能够实现这一点:创建一个额外的字段,然后将其设置为默认排序。
qs = qs.extra(select={'natname':"concat('0', nat, name)"}, order_by=['natname'])
现在,在我的管理界面中,我有其他字段 {name, nat, location, space, ....}
并且表中的结果按 natname
排序当页面加载时...完美。
但现在我希望在 name
字段上启用排序,但我希望它按 natname
排序,而不是按 name
排序。这可能吗?
因此,即使 natname
是一个额外的字段,我想在排序时以某种方式将列 name
与 natname
绑定。
现在,如果我执行 qs.query.__str__()
,我会得到 order by natname
的 sql 查询。当我单击列 name
时,按 name
的更改进行排序,但仅对于这种特殊情况,我希望它按 natname
排序。这可能吗?
我研究了 Django 如何在
下生成这些自动管理页面的标题和视图,但它只引用 list_display
在此模型的 ModelAdmin
中定义的集合。如果我在那里进行任何更改,管理视图中显示的列也会发生更改。
听起来可能有点令人困惑。如果您需要特定详细信息,请随时询问。
谢谢。
So, the extra field in a queryset can be used to add additional columns to your select query, which in turn can be set as the default ordering. I have so far been able to achieve this: created an extra field and then set it as default ordering.
qs = qs.extra(select={'natname':"concat('0', nat, name)"}, order_by=['natname'])
Now, in my admin interface, I have my other fields {name, nat, location, space, ....}
and the results from the table are ordered by natname
when the page is loaded...perfect.
But now I wish to enable ordering on the name
field, but instead of ordering by name
, I want it to order it by natname
. Is this possible?
So even though natname
is an extra field, I want to somehow bind the column name
with natname
when it comes to ordering.
Right now, if I do qs.query.__str__()
, I get the sql query with order by natname
. When I click on the column name
, order by changes to name
, but just for this special case, I wish it to order it by natname
. Is this possible?
I have looked at how Django generates the headers and the views for these automated admin pages under <django-installation-path>/contrib/admin
but it only references the list_display
set defined in the ModelAdmin
for this model. If I make any changes there, the columns displayed get changed in the admin-view.
It might sound a bit confusing. If you require particular details, please feel free to ask.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的!这是可能的(至少在 Django 1.2.3 中):
在你的 admin.ModelAdmin 子类中:
Yes! this is possible (at least in Django 1.2.3):
In your admin.ModelAdmin subclass: