Django REST框架:AttributeError:Serializer对象没有属性'Meta'

时间:2015-01-14 16:45:09

标签: python django django-rest-framework attributeerror

给出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

为什么我在访问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'

我遵循了Serializers tutorial的第一部分,它似乎在呈现的形式中不起作用。我已经尝试删除模型中的Meta类,但仍然会发生错误。

问题原因

出于某种原因,我并没有完全按照提到的 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

2 个答案:

答案 0 :(得分:8)

因为您没有定义Meta课程。

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

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

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

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

答案 1 :(得分:6)

HyperlinkedModelSerializer是一个模型序列化器。因此,您需要定义class Meta,如下所示:

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