Django REST framework: AttributeError: Serializer object has no attribute 'Meta'

Django REST framework: AttributeError: Serializer object has no attribute 'Meta'

给定以下模型和序列化器用于 Django REST framework 设置:

#!/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

为什么我在访问 http://localhost:8000/stationreports/ 时收到此 AttributeError

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'

我遵循了 Serializers tutorial 的第一部分,但在目前的形式中似乎不起作用。我已经尝试删除模型中的 Meta class 但仍然出现错误。

问题原因

出于某种原因,我没有完全遵循提到的 Serializers 教程。当我更改以下内容时,我给出的示例有效:

之前:

class GaugeReportSerializer(serializers.HyperlinkedModelSerializer):
    water_level = serializers.IntegerField(required=True, max_length=5)

之后:

class GaugeReportSerializer(serializers.Serializer):
    water_level = serializers.IntegerField(required=True)

我想这个错误是因为我之前做了Quickstart tutorial

因为您没有定义Meta class。

在 Django REST Framework 中,Meta classes 不是从其父 classes 继承的。

class StationReportSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = StationReport

以上内容应该可以解决您的问题。

http://www.django-rest-framework.org/api-guide/serializers/#inheritance-of-the-meta-class

HyperlinkedModelSerializer 是一个模型序列化器。所以你需要像下面这样定义 class Meta

此外,当您在元 class 上设置 model 时,您不需要在序列化程序上显式定义所有字段。您只需将它们添加到元 class 的 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},
    }