我想传递一个queryset
为JSON
对象:查询集序列化:AttributeError的:“快译通”对象有没有属性“_meta”
structure=Fund.objects.all().values('structure').annotate(total=Count('structure')).order_by('-total')
然而,querysets
不Json Serializable
因此,我修改了代码:
from django.core import serializers
structure=serializers.serialize('json',Fund.objects.all().values('structure').annotate(total=Count('structure')).order_by('-total'))
但我得到这个错误:AttributeError: 'dict' object has no attribute '_meta'
,这是我的查询集:<QuerySet [{'total': 106, 'structure': 'Corp'}, {'total': 43, 'structure': 'Trust'}, {'total': 2, 'structure': 'OM'}, {'total': 0, 'structure': None}]>
豆:https://stackoverflow.com/a/9061105/1571826 –
试了一下已经使用'only'没没有工作。 – anderish
尝试将您的查询集(剥离到值字典)放入顶级字典中,如{'thing':the_queryset}并序列化该对象。有时序列化程序不会让你序列化一个类似列表的东西,因为安全性。 – theWanderer4865