2017-02-23 162 views
0

了代碼AttributeError的: '快譯通' 對象有沒有屬性 '_meta'

shops = Shop.objects.filter(id__in=list(set(shop_ids))).all() 
shop_list = [] 
for s in shops.only(): 
    shop_list.append({ 
     'id': s.id, 
     'name': s.name, 
     'preview': s.preview, 
    }) 

response_data['shop'] = serializers.serialize('json', shop_list) 

return response_data 

.....

AttributeError: dict object has no attribute _meta 

我怎樣才能解決這個問題?

+0

[Django JSON ::'dict'object has no attribute'\ _meta']可能重複(http://stackoverflow.com/questions/9061068/django-json-dict-object-has-no-attribute- meta) – Anomitra

+0

我嘗試做那個帖子,但是錯誤不會爲你的時間隱藏 –

回答

1

序列化期望queryset作爲第二個參數 - 並且您正在傳遞字典列表; docstring:

def serialize(format, queryset, **options): 
    """ 
    Serialize a queryset (or any iterator that returns database objects) using 
    a certain serializer. 
    """ 
    ... 

基本上,您的數據幾乎是序列化的。請撥打簡單json.dumps();

快樂編碼。

+0

ty! –

+0

或者,您可以使用JsonResponse:https://docs.djangoproject.com/en/1.10/ref/request-response/#jsonresponse-objects這可能會更乾淨。 – opalczynski

相關問題