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 4 years, 11 months ago
- CSV to JSON Fixture by briangershon 3 years, 9 months ago
- JsonResponse by zakj 6 years, 1 month ago
- Improved YAML serializer for large databases by rspeer 4 years, 1 month ago
- Retrieve Latitude & Longitude for an Address from Google Geocoder V3 by whardeman 2 years, 6 months ago
Comments
Django provides native means of serialising it's models:
from django.core import serializers data = serializers.serialize("xml", SomeModel.objects.all())
https://docs.djangoproject.com/en/dev/topics/serialization/
#
@siloraptor That won't let you serialise a ValuesQuerySet result
#
Why not converting to a list?
list(MyModel.objects.values('id','title','...','...'))#
Didn't know you could do that. Dicts can be useful if you want to create more complex nested JSON.
#