我有这些序列化器:
class OneSerializer(serializers.ModelSerializer):
class Meta:
model = OneModel
fields = ['time_column', 'event_column']
class TwoSerializer(serializers.ModelSerializer):
survival_columns = OneSerializer(many=True, required=False)
class Meta:
model = UserFile
fields = ['id', 'name', 'survival_columns']
字符串
现在,在create
方法中,我试图从validated_data
中检索survival_columns
值,但**它不存在!**如果我打印POST数据,字段和值将正确显示:
def create(self, validated_data):
print(self.context['request'].POST) # <QueryDict: {'name': ['Clinical data.csv'], 'survival_columns': ['{"event_column":"Test","time_column":"Test"}']}>
print(validated_data) # {'name': 'Clinical data.csv'}
型
为什么要从请求中过滤字段survival_columns
?
1条答案
按热度按时间xkrw2x1b1#
我用FormData发出请求,
survival_columns
字段是一个数组,在发送之前被Stringyfied。正如this article指出的那样,Django REST框架不支持FormData和JSON的这种组合。解决方案是使用
json
内置包手动解码字段:字符串