将django ValuesQuerySet转换为json对象 [英] Converting a django ValuesQuerySet to a json object
本文介绍了将django ValuesQuerySet转换为json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用Django中的ValuesQuerySet功能将从查询返回的字段数限制为仅需要的字段数。我想序列化这个数据集一个JSON对象然而,Django不断抛出一个错误。下面我已经包括我的代码和我收到的错误:
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')
错误:
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
谢谢!
推荐答案
尝试通过使用QuerySet的 serialize
方法将值列表中的字段放在值列表中:
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'))
这篇关于将django ValuesQuerySet转换为json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文