Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a django ValuesQuerySet to a json object

I'm trying to use the ValuesQuerySet feature in Django to limit the number of fields returned from query to only those I need. I would like to serialize this data set a JSON object However, Django keeps throwing an error. Below I've included my code and the error I receive:

objectList = ConventionCard.objects.values('fileName','id').filter(ownerUser = user)
data = serializers.serialize('json', objectList)
return HttpResponse(data, mimetype='application/javascript')

The Error:

Exception Type:     AttributeError
Exception Value:    'dict' object has no attribute '_meta'
Exception Location:     C:\Python27\lib\site-packages\django\core\serializers\base.py in serialize, line 41

Thanks !

like image 704
Karthik Ramachandran Avatar asked Jul 06 '11 18:07

Karthik Ramachandran


4 Answers

Cast the ValuesQuerySet to a list first:

query_set = ConventionCard.objects.values('fileName','id').filter(ownerUser = user)

list(query_set)

Removing the values call as suggested by ars causes the manager to pull all columns from the table, instead of only the two you need.

like image 111
Aaron Avatar answered Nov 05 '22 06:11

Aaron


Try subsetting the fields in your values list through the serialize method using a QuerySet instead:

from django.core import serializers
objectQuerySet = ConventionCard.objects.filter(ownerUser = user)
data = serializers.serialize('json', objectQuerySet, fields=('fileName','id'))
like image 34
ars Avatar answered Nov 05 '22 06:11

ars


I continued to get a dict object has no attribute _meta error when using the list() method above. However I found this snippet that does the trick

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)
like image 44
7wonders Avatar answered Nov 05 '22 05:11

7wonders


Just to add a few details I've found:

When I tried @ars answer specifying the fields, like:

s_logs = serializers.serialize("json", logs, fields=('user', 'action', 'time'))

I get this:

[{"pk": 520, "model": "audit.auditlog", "fields": {"user": 3, "action": "create", "time":"2012-12-16T12:13:45.540"}}, ... ]

Which was not a simple serialization of the values as I wanted it.

So I tried the solution proposed by @Aaron, converting the valuesqueryset to a list, which didn't work the first time because the default encoder cannot deal with floats or datetime objects.

So I used @Aaron solution but using the JSON encoder that is used by django's serializer (DjangoJSONEncoder) by passing it as a kwarg to simplejson.dumps(), like this:

s_logs = list(logs.values('user', 'ip', 'object_name', 'object_type', 'action', 'time'))

return HttpResponse(simplejson.dumps( s_logs, cls=DjangoJSONEncoder ), mimetype='application/javascript')
like image 36
AJJ Avatar answered Nov 05 '22 05:11

AJJ