django - Dynamically add computed column to queryset
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
精彩评论