得到断言错误是我的django(1.8.4)

时间:2018-01-16 13:39:52

标签: python django-models django-rest-framework

from rest_framework import serializers
from .models import Stock


class StockSerializer(serializers.ModelSerializer):

    class Meta:

        model = Stock

        #field = ('ticker','volume')
        field = '__all__'

我收到了一个例外值:("Creating a ModelSerializer without either the 'fields' attribute or the 'exclude' attribute has been deprecated since 3.3.0, and is now disallowed. Add an explicit fields = '__all__' to the StockSerializer serializer.",)

2 个答案:

答案 0 :(得分:5)

正如错误消息所示,所需属性为fields,带有's',而不是field

答案 1 :(得分:3)

我假设您的序列化程序目前看起来像:

class StockerSerializer(serializers.Serializer):
    class Meta:
        model = Stock

Django抱怨的问题是元类需要定义它需要序列化的Stock模型中的哪些字段。您有三种选择:您可以选择fields = (‘some’, ‘fields’,...)exclude = (‘fields’, ‘other’, ‘than’, ‘these’...)fields = ‘__all__’

最简单的选项是最后一个选项,它将使序列化程序序列化Stock模型中的所有字段,这可能是您想要的。然后,您应该将代码修改为:

class StockerSerializer(serializers.Serializer):
    class Meta:
        model = Stock
        fields = ‘__all__’