- June 3, 2011
- 1 (after 1 ratings)
Perhaps this is blindingly obvious but I spent a while trying to get this, before figuring out that when a each item of an iterated ValuesQuerySet is a dict.
Useful for serialising to JSON or XML etc.
1 2 3 4 5 6 7
def ValuesQuerySetToDict(vqs): return [item for item in vqs] # Usage data=MyModel.objects.values('id','title','...','...') data_dict = ValuesQuerySetToDict(data) data_json = simplejson.dumps(data_dict)
More like this
- codigo alto nivel by MrRocklion 1 month ago
- Load template from specific app by Krzysiek555 1 month, 3 weeks ago
- PostgreSQL JSON subqueries by dolamroth 1 month, 3 weeks ago
- "Magic Link" Management Command by webology 6 months, 4 weeks ago
- Closest ORM models to a latitude/longitude point by simonw 7 months ago