简体   繁体   English

验证期间Django Rest Framework序列化程序丢失了ListField数据

[英]Django Rest Framework Serializer lost ListField data during validation

I want to serialize a list of strings as well as some other fields. 我想序列化字符串列表以及其他一些字段。 Somehow it doesn't appear to be in validate data. 某种程度上它似乎不在验证数据中。

class OptionsSerializer(serializers.Serializer):
    lst = serializers.ListField(
        serializers.CharField(),
    )
    field_a = serializers.CharField()
    field_b = serializers.IntegerField()

Request payload: 请求有效负载:

{"lst":["abc"],"field_a":"some text","field_b":1}

Debugger output: 调试器输出:

ipdb> serializer.is_valid()
True
ipdb> serializer.validated_data
OrderedDict([('field_a', 'some text'), ('field_b', 1)])

Any ideas about why is it so? 关于它为什么的任何想法?

Documentation says you need to set child keyword argument of ListField. 文档说您需要设置ListField的child关键字参数。

child - A field instance that should be used for validating the objects in the list. child-一个字段实例,应用于验证列表中的对象。 If this argument is not provided then objects in the list will not be validated. 如果未提供此参数,则将不验证列表中的对象。

Try this: 尝试这个:

lst = serializers.ListField(
    child=serializers.CharField()
)

child is keyword argument, therefore you need to initialize the ListField like this: child是关键字参数,因此您需要像这样初始化ListField

 lst = serializers.ListField(
        child=serializers.CharField(),
 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM