我创建了以下序列化程序:
class KeywordSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Keyword
fields = ('my_field')
但是,当我尝试在 View 集中使用它时,出现以下错误:
The
fields
option must be a list or tuple or "__all__". Got str.
但问题是,fields 是一个元组。我什至尝试将其设置为 all 或列表,但我得到了同样的错误。无论字段名称如何,都会出现相同的错误。
有人知道为什么会这样吗?
最佳答案
是的,在 Python 中 ('my_field')
是一个字符串。要创建单元素元组,您需要一个逗号:('my_field',)
。
请注意,这与 DRF 无关。
关于python - Django REST 框架元组被解释为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35676293/