开发者

How to order a Django QuerySet string property numerically?

开发者 https://www.devze.com 2023-03-19 02:03 出处:网络
I have a QuerySet, and I would like to order them numerically. The issue is the data is store开发者_如何转开发d as a String. I know how to do this in SQL or a list, but not a query set. Is this even p

I have a QuerySet, and I would like to order them numerically. The issue is the data is store开发者_如何转开发d as a String. I know how to do this in SQL or a list, but not a query set. Is this even possible?


You'll probably need to fallback to Django's extra function. Perhaps:

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

(Assuming you're using MySQL)

0

精彩评论

暂无评论...
验证码 换一张
取 消