python - 如何以数字方式订购 Django QuerySet 字符串属性?

标签 python django sorting django-models sql-order-by

我有一个 QuerySet,我想按数字对它们进行排序。问题是数据存储为字符串。我知道如何在 SQL 或列表中执行此操作,但不知道如何在查询集中执行此操作。这可能吗?

最佳答案

您可能需要回退到 Django 的 extra功能。也许:

ordered = (qs.extra(select={"order_column": "CONVERT(column, INTEGER)"})
           .order_by("order_column"))

(假设您使用的是 MySQL)

关于python - 如何以数字方式订购 Django QuerySet 字符串属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6645781/

相关文章:

python - Django:make_password 输出的密码与 django.auth 不同

django - 类型错误 : __init__() got an unexpected keyword argument in Django Form & Formset

c++ - 排序 vector : how to have the boolian function accept a third variable

python - 光标删除字符——Spyder程序

python - 如何让 scons 调用外部脚本?

python - LocaleMiddleware 在非 i18n url 模式上重定向

c++ - 带有跳跃列表的双向链表以排序方式插入

java - 如何按类对 arrayList 中的对象进行排序?

python - 在数组中查找封闭空间

python - 没有文件名的文件的md5sum分配给python中的变量