REST中的多对多字段和嵌套序列化器:覆盖嵌套序列化器不会在Django中创建嵌套对象

时间:2019-06-27 11:49:01

标签: django django-models django-rest-framework django-serializer

我有两个具有多对多关系的模型, 我正在尝试将嵌套数据发送到我的API。不幸的是,它只给了我一个空数组。

这是我正在尝试的:

我的模特:


class Building(models.Model):
    name  = models.CharField(max_length=120, null=True, blank=True)
    net_leased_area = models.FloatField(null=True, blank=True)

class BuildingGroup(models.Model):
    description           = models.CharField(max_length=500, null=True, blank=True)
    buildings             = models.ManyToManyField(Building, default=None, blank=True)

我的通用API视图:

class BuildingGroupCreateAPIView(CreateAPIView):
    queryset                    = BuildingGroup.objects.all()
    serializer_class            = BuildingGroupSerializer

我的序列化器:


class BuildingGroupSerializer(serializers.ModelSerializer):

    buildings = BuildingSerializer(many=True)

    class Meta:

        model = BuildingGroup

        fields = (
            'description',
            'buildings',
        )

    def create(self, validated_data):
        buildings_data = validated_data.pop('buildings')
        building_group = BuildingGroup.objects.create(**validated_data)
        for building_data in buildings_data:
            Building.objects.create(building_group=building_group, **building_data)
        return building_group

当我发送数据时,它返回以下内容:


{"description":"Test Description API","buildings":[]}

在数组中,我希望有我的字典数组。

当我覆盖create方法以发送嵌套对象时,我尝试遵循REST文档。 (https://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers),我以为我做得正确,但是史诗失败。

我使用这样的自定义方法通过请求发送数据:

test_api_local(method="post", data={
        "description": "Test Description API",
        "buildings": [{'name' : 'Testname'}, .... ],
         })

我们非常感谢您的帮助。非常感谢!

编辑:当我尝试在REST视图上对其进行测试时,它会告诉我:

TypeError: 'building_group' is an invalid keyword argument for this function

EDIT2:这是我的观点:

class BuildingGroupCreateAPIView(CreateAPIView):
    queryset                    = BuildingGroup.objects.all()
    serializer_class            = BuildingGroupSerializer

    def create(self, request, *args, **kwargs):
        serializer = BuildingGroupSerializer(data=self.request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

1 个答案:

答案 0 :(得分:1)

您必须根据有效负载数据中传递的ID显式获取或创建Building实例,然后将它们添加到BuildingGroup实例中。

class NestedBuildingSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(required=False)

    class Meta:
        model = Building
        fields = '__all__'


class BuildingGroupSerializer(serializers.ModelSerializer):
    buildings = NestedBuildingSerializer(many=True)

    class Meta:
        model = BuildingGroup
        fields = (
            'description',
            'buildings',
        )

    def create(self, validated_data):
        buildings_data = validated_data.pop('buildings')
        building_group = BuildingGroup.objects.create(**validated_data)
        buildings = []  # it will contains list of Building model instance
        for building_data in buildings_data:
            building_id = building_data.pop('id', None)
            building, _ = Building.objects.get_or_create(id=building_id,
                                                         defaults=building_data)
            buildings.append(building)
        # add all passed instances of Building model to BuildingGroup instance
        building_group.buildings.add(*buildings)
        return building_group

class BuildingGroupView(ListAPIView, CreateAPIView):
    queryset = BuildingGroup.objects.all()
    serializer_class = BuildingGroupSerializer


## Assume you add your views like this in urls.py
urlpatterns = [
    .....
    path('building-groups', views.BuildingGroupView.as_view(),
         name='building-group'),
    .....
]

POST 方法调用端点/building-groups时,其有效载荷如下:

{
  "description": "here description",
  "buildings": [
    {
      "id": 1, # existing building of id 1
      "name": "name of building 1",
      "net_leased_area": 1800
    },
    { 
      # a new building will gets create
      "name": "name of building 2",
      "net_leased_area": 1800
    }
  ]
}

然后,它将返回如下响应:-

{
  "description": "here description",
  "buildings": [
    {
      "id": 1,
      "name": "name of building 1",
      "net_leased_area": 1800
    },
    {
      "id": 2
      "name": "name of building 2",
      "net_leased_area": 1800
    }
  ]
}

了解有关M2M relationship的信息 并且.get_or_create()

  

注意:BuildingSerializerNestedBuildingSerializer都不同。不要把它们混在一起。