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.",)
答案 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__’