Django : json serialize a queryset which uses defer() or only()

Posted by PlanetUnknown on Stack Overflow See other posts from Stack Overflow or by PlanetUnknown
Published on 2010-01-19T19:56:59Z Indexed on 2010/04/04 16:03 UTC
Read the original article Hit count: 444

Filed under:
|
|

Now I've been using json serializer and it works great.

I had to modify my queries where I started using the only() & defer() filters, like so -

retObj = OBJModel.objects.defer("create_dt").filter(loged_in_dt__gte=dtStart)

After I've done the above, suddenly the json serializer is returning empty fields -

{"pk": 19047, "model": "OBJModel_deferred_create_dt", "fields": {}}

If I remove the defer(), the serializer gives all the data correctly.

import json
from django.utils import simplejson
from django.core import serializers
json_serializer = serializers.get_serializer("json")()
retObj = OBJModel.objects.defer("create_dt").filter(loged_in_dt__gte=dtStart)
json_serializer.serialize(retObj, ensure_ascii=False)

I've scratched my head on this for a while now. Any insight would be great.

NOTE : I am using django 1.1

© Stack Overflow or respective owner

Related posts about django

Related posts about JSON