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:
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
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'))
Cast the ValuesQuerySet to a list first:
query_set = ConventionCard.objects.values('fileName','id').filter(ownerUser = user) list(query_set)
values call as suggested by ars causes the manager to pull all columns from the table, instead of only the two you need.