简体   繁体   English

Django REST框架:嵌套的序列化程序和反序列化

[英]Django REST Framework: nested serializers and deserializing

I'm again stuck with Django REST Framework and its serializers. 我再次陷入Django REST Framework及其序列化器的困境。

Basically, what I want to be able to do is stick the following incoming data into a serializers.Serializer instance: 基本上,我想做的就是将以下传入数据粘贴到serializers.Serializer实例中:

data = {
    "thing_id": 715,
    "sub_things": [
        {
            "id": 1,
            "name": "bob"
        },

        {
             "id": 2,
             "name": "mike"
        }
    ]
}

sub_things are handled by a serializers.ModelSerializer called SubThingSerializer. sub_things由称为SubThingSerializer的serializers.ModelSerializer处理。 This is how it looks like. 这就是它的样子。

class SubThingSerializer(serializers.ModelSerializer):
    class Meta:
        model = SubThing
        fields = ('id', 'name')
        read_only_fields = ('id', 'name')

Serialization of Thing is handled by a ThingSerializer, which I've for now handled as below: Thing的序列化由ThingSerializer处理,我现在如下处理:

class ThingSerializer(serializers.Serializer):
    thing_id = serializers.IntegerField()
    sub_things= SubThingSerializer(many=True)

Now, when I do 现在,当我做

serializer = ThingSerializer(data=data)

I get empty OrderedDicts like: 我得到空的OrderedDicts像:

{'sub_things': [OrderedDict(), OrderedDict()], 'thing_id': 715}

I guess it's wise to say that ThingSerializer will not need to get stored into a database, but it does use sub_things from database. 我想说ThingSerializer不需要存储到数据库中是很明智的,但是它确实使用了数据库中的sub_things。 These will not be written into db either. 这些也不会写入db。 This is just to keep track of what sub_things the thing contains, pass this data back and forth between a browser client and a Python object for some calcualtions. 这只是为了跟踪事物所包含的子事物,在浏览器客户端和Python对象之间来回传递此数据以进行某些计算。 And maybe store it in the session. 并可能将其存储在会话中。

edit 1: 编辑1:

Most likely there's something I need to add to the create method of my ThingSerializer. 很可能我需要在ThingSerializer的create方法中添加一些内容。 So, I guess the ultimate question is: what's the correct syntax to pass sub_thing data to SubThingSerializers? 因此,我猜最终的问题是:将sub_thing数据传递给SubThingSerializers的正确语法是什么?

edit 2: 编辑2:

I dug a bit more. 我再挖一点。 Seems that empty OrderedDicts get passed in validated_data to ThingSerializer.create() for some reason. 似乎出于某种原因,空的OrderedDicts会在validated_data中传递给ThingSerializer.create()。 Serializer validates alright with serializer.is_valid(). 序列化程序使用serializer.is_valid()验证一切正常。 I could access the necessary data from initial_data, but that doesn't seem very solid. 我可以从initial_data访问必要的数据,但这似乎不是很可靠。

edit 3: 编辑3:

Also tested around with serializers.ListField() for sub_things. 还对sub_things使用serializers.ListField()进行了测试。 Still seeing empty OrderedDicts in validated_data inside the create method. 在create方法内的validated_data中仍然看到空的OrderedDicts。

Finally figured this out. 终于明白了。 read_only_fields on my SubThingSerializer prevented the data from getting through the validation resulting in empty dicts being created. 我的SubThingSerializer上的read_only_fields阻止数据通过验证,从而导致创建空字典。 I used read_only_fiels to prevent non-existent sub_thing data from being passed to my code, but I guess I'll need to find another way. 我使用read_only_fiels来防止将不存在的sub_thing数据传递到我的代码中,但是我想我需要寻找另一种方法。

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

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