Hi
I have a ORM query that results something like this:
+---------+-----+-------+
| date | key | value |
+---------+-----+-------+
| 201312 | A | 123 |
| 201312 | B | 223 |
| 201312 | C | 133 |
| 201311 | A | 173 |
| 201311 | B | 463 |
| 201311 | C | 463 |
+---------+-----+-------+
As far as i know, django cannot transpose a query so i need do get this into the following format in another way:
+---------+-----+-----+-----+
| date | A | B | C |
+---------+-----+-----+-----+
| 201312 | 123 | 223 | 133 |
| 201311 | 173 | 463 | 463 |
+---------+-----+-----+-----+
I tried something like a loop and the setdefault(), but my knowledge in python seems to be too poor.
kzlist = XYZ.objects.filter(...).values_list('date', 'key', 'value')
data = {}
for kz in klist:
data.setdefault(kz['date'], {}).update({'%s' % kz['key'] : kz['value']})
Has anyone an advice to solve this little problem?
-- I have a ORM query that results something like this:
+---------+-----+-------+
| date | key | value |
+---------+-----+-------+
| 201312 | A | 123 |
| 201312 | B | 223 |
| 201312 | C | 133 |
| 201311 | A | 173 |
| 201311 | B | 463 |
| 201311 | C | 463 |
+---------+-----+-------+
As far as i know, django cannot transpose a query so i need do get this into the following format in another way:
+---------+-----+-----+-----+
| date | A | B | C |
+---------+-----+-----+-----+
| 201312 | 123 | 223 | 133 |
| 201311 | 173 | 463 | 463 |
+---------+-----+-----+-----+
I tried something like a loop and the setdefault(), but my knowledge in python seems to be too poor.
kzlist = XYZ.objects.filter(...).values_list('date', 'key', 'value')
data = {}
for kz in klist:
data.setdefault(kz['date'], {}).update({'%s' % kz['key'] : kz['value']})
Has anyone an advice to solve this little problem?
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d3d3247f-57f6-49fa-9ec0-df25a6690e71%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment