tags:

views:

65

answers:

1

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.01.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.

A: 

annotate should help you:

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

gruszczy
Hmmm - it looks like annotate is particularly concerned with aggregation. In this case, I'm not interested in adding an aggregated column - it's more of a re-formatting problem really. I can't find examples of annotate being used in this context.While reading up on annotate, I did discover that annotate fields don't get serialized, so this probably means I back to doing the re-formatting on the SQL side anyways.
Ferg
Why do you want to do it in SQL? Can't you just write some python or template code?
alex vasi
My template is using ExtJs and as such, I'm passing in JSON from the django serializer. This is generated directly from the querset, so I need to get the calculated field into the queryset somehow.And, yes, I'm trying to find out if there is a way of doing that in Python.
Ferg
Can you simply iterate over queryset and add generated fields? Won't they be serialized then?
gruszczy