我正在尝试使用Django中的ValuesQuerySet功能将查询返回的字段数限制为仅需要的字段数。我想将此数据集序列化为JSON对象。但是,Django不断抛出错误。下面包含了我的代码和收到的错误:
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:
serialize
from django.core import serializers objectQuerySet = ConventionCard.objects.filter(ownerUser = user) data = serializers.serialize('json', objectQuerySet, fields=('fileName','id'))