开发者

django - Dynamically add computed column to queryset

开发者 https://www.devze.com 2023-01-08 05:34 出处:网络
I have a database table containing 4 integer columns representing version information - Major, Minor, Cust, Rev.

I have a database table containing 4 integer columns representing version information - Major, Minor, Cust, Rev.

I'd like to represent these as a single column in my form, with relevant padding. So: 0, 1, 2, 3 = [0.0开发者_运维知识库1.002.0003].

I realise that there are several ways to use SQL to do this, but I'm curious as to whether it's possible to do it on the django side? So, after:

queryset = MyModel.objects.all()

...can I then extend the queryset with an additional calculated field? Later I pass the Queryset to be serialized, and I need the calculated field present at this stage, so I don't think an attribute will work.


annotate should help you:

http://docs.djangoproject.com/en/dev/ref/models/querysets/#annotate-args-kwargs

0

精彩评论

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