Django rest framework 如何使用DRF序列化程序来使用多个序列化程序,并使发送的数据平坦化

Django rest framework 如何使用DRF序列化程序来使用多个序列化程序,并使发送的数据平坦化,django-rest-framework,django-serializer,Django Rest Framework,Django Serializer,我正在努力找出编写这些序列化程序的最佳方法 我有一个这样的模型 class Foo(models.Model): db = models.CharField(max_length=30) name = models.CharField(max_length=30) schema = models.CharField(max_length=50) table_name = models.CharField(max_length=50) ext_type =

我正在努力找出编写这些序列化程序的最佳方法

我有一个这样的模型

class Foo(models.Model):
    db = models.CharField(max_length=30)
    name = models.CharField(max_length=30)
    schema = models.CharField(max_length=50)
    table_name = models.CharField(max_length=50)
    ext_type = models.CharField(max_length=100)
    sample_by = models.CharField()
    sample_by_val = models.CharField()
 class SaveSerializer(serializers.ModelSerializer): #
     """
        Cannot figure out the best way to do this.
        This is just an example serializer I had in mind.
        I want to understand a cleaner/best way to do this.
     """
   sample = SampleSerializer()
   exts = ExtSerializer()

   class Meta:
     model = Foo
接下来,我有一个类似这样的序列化程序

class Foo(models.Model):
    db = models.CharField(max_length=30)
    name = models.CharField(max_length=30)
    schema = models.CharField(max_length=50)
    table_name = models.CharField(max_length=50)
    ext_type = models.CharField(max_length=100)
    sample_by = models.CharField()
    sample_by_val = models.CharField()
 class SaveSerializer(serializers.ModelSerializer): #
     """
        Cannot figure out the best way to do this.
        This is just an example serializer I had in mind.
        I want to understand a cleaner/best way to do this.
     """
   sample = SampleSerializer()
   exts = ExtSerializer()

   class Meta:
     model = Foo
问题在于-此视图中的数据是以这种格式发送的

{
   "sample": {"sample_by":"weight", "sample_by_val":[30,50,60],"name":"test"}
   "exts": {"ext_type": "BY WEIGHT", "table":"EMPLOYEE"},
   "db" : "foobar",
   "schema" : "abcd"
}

class ExtSerializer(serializers.Serializer):
  table = serializers.CharField()
  ext_type = serializers.CharField()

class SampleSerializer(serializers.Serializer):
  sample_by = serializers.CharField()
  # Similar ones for sample_by_val and name
我正在努力编写
SaveSerializer
。它需要首先检查以特定格式发送的数据,并将其通过一些序列化程序传递。然后,它需要将该数据格式展平,以将其存储在模型本身中。
有人能帮我理解最好的方法吗?这将是非常有帮助的!谢谢

您可以重写to_表示()和to_internal_value(),我认为这是最好的方法。

抱歉,我要收回我的话,只需像这样重写create方法:

#override create method of serializer, you can easily handle object creation.
def create(self, validated_data):
    #first, pop sample and exts from validated data.
    sample = validated_data.pop('sample')
    exts = validated_data.pop('exts')

    #then, create object
    foo = Foo.objects.create(**validated_data, **sample, **exts)  #flat validated_data, sample, and exts to create Foo.
    return foo

#maybe you also want to handle representation, since the model of Foo doesn't has ext and sample fields
def to_representation(self, instance):
    sample = SampleSerializer(instance).data #SampleSerializer will deserialize sample_by field of Foo instance.
    exts = ExtSerializer(instance).data
    instance.sample = sample
    instance.exts = exts
    return super(SaveSerializer, self).to_representation(instance)

你能给我看一些如何使用它的代码吗?我有点困惑。