Here is the problem: Django’s serializer doesn’t support dictionaries and simplejson doesn’t support Django Querysets. See JSON Serializing Django Models with simplejson
I was wondering if there is anything wrong with my solution. I have something like:
people = People.objects.all().values('name', 'id')
json.dumps(list(people))
I am still a newbie with Python/Django. Is casting the QuerySet to a list a bad idea? Is it more efficient to use the DjangoJSONEncoder suggested in the other topic?
Your solution is totally valid and very clean in my own opinion.
If you need a list of lists (instead of a list of dictionaries) you can use too:
Sometimes when the json output is very complex we usually use a json template with the *render_to_string* function, for example:
The template people.json could be:
But the use of templates is reserved for more complex cases than yours. I think that for easier problems a good solution is to use simplejson.dumps function.