更改 Django 管理员对整数排序的方式?
该模型有一个 IntegerField
且 null=True, Blank=True
,因此它允许该字段的值为 None
。对列进行排序时,“无”始终被视为最低值。是否有可能让 Django 将它们视为最高值?因此,当按降序排序时,而不是:
100 43 7 None
它将是:
None 100 43 7
我意识到我可以分配一个极高的数字而不是 None ,但为了整洁起见,我想知道是否还有其他选项。
谢谢!
The model has an IntegerField
with null=True, blank=True
, so it allows the field to have a value of None
. When the column is sorted, the Nones are always treated as the lowest values. Is it possible to make Django treat them as the highest values? So when sorted in descending order, instead of:
100 43 7 None
It would go:
None 100 43 7
I realize I could assign an extremely high number instead of None, but for neatness' sake, I was wondering if there were any other options.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Daniel 是正确的,数据库决定排序顺序,不同的数据库对待 NULL 的顺序不同。然而,有一些方法可以让数据库给你你想要的顺序。
PostgreSQL 足够好,实际上允许您将“NULLS FIRST”或“NULLS LAST”附加到查询中(参考)。
对于 MySQL 和 SQLite,有一个替代方案(也适用于 PostgreSQL),如 此处。本质上,如果您希望最后使用空值,您可以这样做:
但是,让 Django 执行这些查询完全是另一回事。在 Django 1.2 中,模型管理器现在有一个 raw() 方法,记录在 此处< /a>,它返回一个 RawQuerySet,它类似于 QuerySet,但不能堆叠(例如,您不能在其中添加 filter() 调用)。当然,您可以将查找参数添加到 SQL 中,而不是堆叠。此功能对您是否有用取决于您想要完成的任务。如果您只是想按该顺序获取模型,然后将查询集传递给视图或其他内容,您可以这样做:
但是,如果您希望这是在管理等中使用的默认顺序,则需要采用不同的方法,也许通过覆盖经理。
Daniel is correct in that the database determines the sort order, and different databases treat the ordering of NULLs differently. However, there are ways to get the DB to give you the order you want.
PostgreSQL is nice enough to actually allow you to append "NULLS FIRST" or "NULLS LAST" to your query (ref).
For MySQL and SQLite, there's an alternative (which will also work in PostgreSQL), as described here. Essentially, if you want nulls last, you would do:
However, getting Django to execute these queries is a different story alltogether. In Django 1.2, model managers now have a raw() method, documented here, which returns a RawQuerySet, which is like a QuerySet, but can't be stacked (e.g. you can't add a filter() call in there). Of course, instead of stacking, you can just add your lookup parameters to the SQL. Whether or not this functionality is useful to you depends on what you're trying to accomplish. If you simply want to fetch your models in that order then pass the queryset to a view or something, you can do:
If however you want this to be the default ordering for use in the admin and such, you'll need a different approach, perhaps via overriding the manager.
决定排序顺序的不是 Django,而是数据库。数据库对于如何对 NULL 进行排序有自己的规则。
一种(相当复杂)的可能性是实现 自定义字段 使用自定义数据库类型以正确的顺序排序。如何执行此操作的详细信息可能取决于您的数据库。
It's not Django that determines the sort order, but the database. And databases have their own rules about how to sort NULLs.
One (rather complicated) possibility would be to implement a custom field that uses a custom database type to sort in the correct order. The details of how you would do this are likely to depend on your database.
由于我使用的是 Django 1.1 并且无法使用
raw()
,最简单的方法是创建一个“int_sort”字段,并用IntegerField 的值填充它
,除非遇到None
,在这种情况下它将采用sys.maxint
的值。然后,在
admin.py
中,我将admin_order_field
设置为“int_sort”字段。Since I'm using Django 1.1 and couldn't use
raw()
, the simplest way turned out to be to create a "int_sort" field, and populate it with the value of theIntegerField
, unless it encountered aNone
, in which case it would take the value ofsys.maxint
.Then, in
admin.py
, I set theadmin_order_field
to be the "int_sort" field.