【发布时间】:2018-02-14 04:17:22
【问题描述】:
我正在尝试将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}]>
【问题讨论】:
-
已经尝试过了,使用
only不起作用。 -
尝试将您的查询集(精简到值字典)放入顶级字典,如 {'thing': the_queryset} 并序列化该对象。有时序列化程序不会让你序列化类似列表的东西,因为安全性。