DjangoRestFramework 类序列化程序缺少“元”属性?
Posted
技术标签:
【中文标题】DjangoRestFramework 类序列化程序缺少“元”属性?【英文标题】:DjangoRestFramework Class Serializers missing "Meta" attribute? 【发布时间】:2015-08-23 02:41:18 【问题描述】:我想在 django 应用程序中使用 DjangoRestframework 但调用时出错?
打电话时,我会回复我:
Exception Type: AssertionError
Exception Value: Class CrawlerSerializers missing "Meta" attribute
但是我在serializer
类中写了元类
请帮帮我
serializers.py 文件:
from rest_framework import serializers
from models import *
class CrawlerSerializers(serializers.ModelSerializer):
class meta:
model = Crawler
fields = ('source','category','template','url')
我的 View.py 文件:
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from models import *
from serializers import *
# Create your views here.
@api_view(['GET','POST'])
def crawler_list(request):
'''
list all task, or create a new task.
'''
if request.method == 'GET':
crawler = Crawler.objects.all()
serializer = CrawlerSerializers(crawler)
return Response(serializer.data)
elif request.method == "POST":
serializer = CrawlerSerializers(data=request.Data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data,status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
如何解决?
【问题讨论】:
【参考方案1】:将 meta 替换为 Meta
class Meta: #instead of meta write Meta (Capital M)
model = Crawler
fields = ('source','category','template','url')
【讨论】:
【参考方案2】:meta
不等于 Meta
。 Python 区分大小写。
【讨论】:
以上是关于DjangoRestFramework 类序列化程序缺少“元”属性?的主要内容,如果未能解决你的问题,请参考以下文章
DjangoRestFramework - 如何使用模型序列化程序访问 OneToOneField 反向关系的其他字段?
djangorestframework 序列化程序错误:u'non_field_errors': [u'No input provided']