Python Django REST 框架:AttributeError: Serializer 对象没有属性“Meta”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27948136/
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
Django REST framework: AttributeError: Serializer object has no attribute 'Meta'
提问by JJD
Given the following model and serializer for a Django REST frameworksetup:
给定以下用于Django REST 框架设置的模型和序列化程序:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.db import models
class StationReport(models.Model):
water_level = models.IntegerField(max_length=5, blank=False)
user_name = models.CharField(max_length=256, blank=False)
email_address = models.CharField(max_length=256, blank=True)
recorded_at = models.DateTimeField(blank=False)
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now_add=True)
...
...
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from rest_framework import serializers
from models import StationReport
class StationReportSerializer(serializers.HyperlinkedModelSerializer):
water_level = serializers.IntegerField(required=True)
user_name = serializers.CharField(required=True)
email_address = serializers.CharField(required=False)
recorded_at = serializers.DateTimeField(required=True)
def create(self, validated_data):
return StationReport.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.water_level = validated_data.get('water_level', instance.water_level)
instance.user_name = validated_data.get('user_name', instance.user_name)
instance.email_address = validated_data.get('email_address', instance.email_address)
instance.recorded_at = validated_data.get('recorded_at', instance.recorded_at)
instance.save()
return instance
Why am I receiving this AttributeError
when I visit http://localhost:8000/stationreports/
?
为什么AttributeError
我访问时会收到这个http://localhost:8000/stationreports/
?
AttributeError at /stationreports/
'StationReportSerializer' object has no attribute 'Meta'
Request Method: GET
Request URL: http://localhost:8000/stationreports/
Django Version: 1.7.3
Exception Type: AttributeError
Exception Value:
'StationReportSerializer' object has no attribute 'Meta'
I followed the first part of the Serializerstutorialwhich does not seem to work in the presented form. I already tried to remove the Meta
class in the model but still the error occurs.
我遵循了Serializers教程的第一部分,该部分似乎不适用于呈现的形式。我已经尝试删除Meta
模型中的类,但仍然发生错误。
The problem reason
问题原因
For some reason I did not exactly follow the mentioned Serializerstutorial. My given example works when I change the following:
出于某种原因,我没有完全遵循提到的序列化程序教程。当我更改以下内容时,我给出的示例有效:
Before:
前:
class GaugeReportSerializer(serializers.HyperlinkedModelSerializer):
water_level = serializers.IntegerField(required=True, max_length=5)
After:
后:
class GaugeReportSerializer(serializers.Serializer):
water_level = serializers.IntegerField(required=True)
I think this mistake happened because I did the Quickstarttutorialbefore.
我认为发生这个错误是因为我之前做过快速入门教程。
采纳答案by dursk
Because you don't have a Meta
class defined.
因为你没有Meta
定义一个类。
In Django REST Framework, Meta
classes are not inherited from their parent classes.
在 Django REST Framework 中,Meta
类不是从它们的父类继承的。
class StationReportSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = StationReport
The above should solve your problem.
以上应该可以解决您的问题。
http://www.django-rest-framework.org/api-guide/serializers/#inheritance-of-the-meta-class
http://www.django-rest-framework.org/api-guide/serializers/#inheritance-of-the-meta-class
回答by Sam Rad
HyperlinkedModelSerializer
is a model serializer. So you need to define the class Meta
like below:
HyperlinkedModelSerializer
是一个模型序列化器。所以你需要定义class Meta
如下:
Moreover, when you set the model
on meta class, then you don't need to explicitly define all of the fields on serializer. You just add them to the fields
of meta class. Unless your are doing something specialized on the fields.
此外,当您设置model
on 元类时,您不需要显式定义序列化程序上的所有字段。您只需将它们添加到fields
元类中即可。除非您正在从事该领域的专业工作。
class StationReportSerializer(serializers.HyperlinkedModelSerializer):
# ...
class Meta:
model = StationReport
fields = (
'water_level',
'user_name',
'email_address',
'recorded_at',
)
extra_kwargs = {
'water_level': {'required': True},
'user_name': {'required': True},
'email_address': {'required': False},
'recorded_at': {'required': True},
}