更改 Django 管理员对整数排序的方式?

发布于 2024-09-09 16:16:39 字数 292 浏览 4 评论 0原文

该模型有一个 IntegerFieldnull=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 技术交流群。

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

发布评论

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

评论(3

救星 2024-09-16 16:16:39

Daniel 是正确的,数据库决定排序顺序,不同的数据库对待 NULL 的顺序不同。然而,有一些方法可以让数据库给你你想要的顺序。

PostgreSQL 足够好,实际上允许您将“NULLS FIRST”或“NULLS LAST”附加到查询中(参考)。

SELECT * FROM table_name ORDER BY int_field DESC NULLS FIRST;

对于 MySQL 和 SQLite,有一个替代方案(也适用于 PostgreSQL),如 此处。本质上,如果您希望最后使用空值,您可以这样做:

SELECT *, int_field IS NULL AS is_null FROM table_name ORDER BY is_null DESC, int_field DESC;

但是,让 Django 执行这些查询完全是另一回事。在 Django 1.2 中,模型管理器现在有一个 raw() 方法,记录在 此处< /a>,它返回一个 RawQuerySet,它类似于 QuerySet,但不能堆叠(例如,您不能在其中添加 filter() 调用)。当然,您可以将查找参数添加到 SQL 中,而不是堆叠。此功能对您是否有用取决于您想要完成的任务。如果您只是想按该顺序获取模型,然后将查询集传递给视图或其他内容,您可以这样做:

YourModel.objects.raw('SELECT *, int_field IS NULL AS is_null FROM table_name ORDER BY is_null DESC, int_field DESC')

但是,如果您希望这是在管理等中使用的默认顺序,则需要采用不同的方法,也许通过覆盖经理。

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).

SELECT * FROM table_name ORDER BY int_field DESC NULLS FIRST;

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:

SELECT *, int_field IS NULL AS is_null FROM table_name ORDER BY is_null DESC, int_field DESC;

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:

YourModel.objects.raw('SELECT *, int_field IS NULL AS is_null FROM table_name ORDER BY is_null DESC, int_field DESC')

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.

瑕疵 2024-09-16 16:16:39

决定排序顺序的不是 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.

千纸鹤带着心事 2024-09-16 16:16:39

由于我使用的是 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 the IntegerField, unless it encountered a None, in which case it would take the value of sys.maxint.

Then, in admin.py, I set the admin_order_field to be the "int_sort" field.

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