- June 3, 2011
- json serialization valuesqueryset
- 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
- JSON encoding middleware by kcarnold 8 years, 2 months ago
- serialize model object with related objects to dict by zakovinko 5 years, 6 months ago
- JsonResponse by zakj 9 years, 4 months ago
- Modelaware json serializer by fivethreeo 9 years, 5 months ago
- Improved YAML serializer for large databases by rspeer 7 years, 4 months ago