- 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 6 years, 8 months ago
- serialize model object with related objects to dict by zakovinko 4 years, 1 month ago
- JsonResponse by zakj 7 years, 11 months ago
- Modelaware json serializer by fivethreeo 7 years, 11 months ago
- Improved YAML serializer for large databases by rspeer 5 years, 10 months ago