我试图将queryset
作为JSON
对象传递:
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}]>
你可以试试:
Django核心序列化程序只能序列化一个
queryset
。但是values()
不返回queryset
,而是返回ValuesQuerySet
对象。您可以指定要在serialize()
方法中的values()
中使用的字段,如下所示:相关问题 更多 >
编程相关推荐