我想傳遞一個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