代码之家  ›  专栏  ›  技术社区  ›  Junior Gantin

如何通过显式添加对象的字段在Django装置中添加关系?

  •  3
  • Junior Gantin  · 技术社区  · 7 年前

    class Component(models.Model):
        name = models.CharField(unique=True, max_length=255)
        component_properties = models.ManyToManyField(ComponentProperty)
    
    class ComponentProperty(models.Model):
        label = models.CharField(unique=True, max_length=255)
        component_type = models.CharField(max_length=255)
    

    和序列化程序:

    class ComponentSerializer(serializers.ModelSerializer):
        class Meta:
            model = Component
            fields = ('name', 'component_properties')
            depth = 1
    
    class ComponentPropertySerializer(serializers.ModelSerializer):
        class Meta:
            model = ComponentProperty
            fields = ('label', 'component_type')
    

    [
       {
          "pk":1,
          "model":"api.componentproperty",
          "fields":{
             "label":"label",
             "component_type":"text"
          }
       },
       {
          "pk":2,
          "model":"api.componentproperty",
          "fields":{
             "label":"description",
             "component_type":"text",
    
          }
       },
       {
          "pk":1,
          "model":"api.component",
          "fields":{
             "name":"text",
             "component_properties":[
                1,
                2
             ]
          }
       }
    ]
    

    干得好!但我有20个固定装置要装。我希望fixture(例如component.json)如下所示:

    [
       {
          "pk":null,
          "model":"api.component",
          "fields":{
             "name":"text",
             "component_properties":[
                {
                   "pk":null,
                   "model":"api.componentproperty",
                   "fields":{
                      "label":"label",
                      "component_type":"text"
                   }
                },
                {
                   "pk":null,
                   "model":"api.componentproperty",
                   "fields":{
                      "label":"description",
                      "component_type":"text",
    
                   }
                }
             ]
          }
       }
    ]
    

    here . 请问,有谁能帮我写这些没有pk的装置和上面描述的关系吗?

    1 回复  |  直到 6 年前
        1
  •  2
  •   wencakisa    7 年前

    据我所知,你想表演 属于 Component properties ComponentSerializer .

    如果你使用你的 ComponentPropertySerializer :

    class ComponentPropertySerializer(serializers.ModelSerializer):
        class Meta:
            model = ComponentProperty
            fields = ('label', 'component_type')
    
    
    class ComponentSerializer(serializers.ModelSerializer):
        # Django REST Framework supports nested serialization
        # You serialize your *component_properties* with your ComponentPropertySerializer
        # As they are many properties, you use *many=True*
    
        component_properties = ComponentPropertySerializer(many=True, read_only=True)
    
        class Meta:
            model = Component
            fields = ('name', 'component_properties')
            depth = 1
    

    我认为这应该再现所需的输出。嵌套序列化在 Django REST Framework's docs .