开发者

How to order a Django QuerySet string property numerically?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜