Django JSON::'dict' 对象没有属性 '_meta'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9061068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:10:03  来源:igfitidea点击:

Django JSON:: 'dict' object has no attribute '_meta'

djangojson

提问by user1003121

def display_home(request):
    from datetime import *
    now=datetime.today()
    print 'Month is %s'%now.month

events=Event.objects.filter(e_date__year=datetime.today().year).filter(e_date__month=datetime.today().month,e_status=1).values('e_name','e_date')
return render_to_response("SecureVirtualElection/home.html",{'events': serializers.serialize("json",events, fields=('e_name','e_date'))},context_instance=RequestContext(request))

error :: 'dict' object has no attribute '_meta'

错误 :: 'dict' 对象没有属性 '_meta'

回答by DrTyrsa

events=Event.objects.filter(e_date__year=datetime.today().year).filter(e_date__month=datetime.today().month,e_status=1).only('e_name','e_date')

Serializer waits for normal queryset, not ValuesQuerySet(which is returned by values). If you want to query only certain fileds, use only.

序列化程序等待正常的查询集,而不是ValuesQuerySet(由 返回values)。如果您只想查询某些字段,请使用only.