I have two models with a many to many relation and I am trying to send nested data to my API. Unfortunately it only gives me back an empty array.
This is what I am trying:
my models:
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)
My generic API view:
class BuildingGroupCreateAPIView(CreateAPIView):
queryset = BuildingGroup.objects.all()
serializer_class = BuildingGroupSerializer
My serializer:
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
When I send data it returns this:
{"description":"Test Description API","buildings":[]}
In the array I would like to have my array of dictionaries.
I am trying to follow the REST documentation here when I am overwriting the create method to send a nested object. (https://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers) and I thought I am doing this correctly, but epic fail.
I send data with request with my custom method like this:
test_api_local(method="post", data={
"description": "Test Description API",
"buildings": [{'name' : 'Testname'}, .... ],
})
Any help is very appreciated. Thanks so much!!
EDIT: When I try to test it on the REST view it tells me:
TypeError: 'building_group' is an invalid keyword argument for this function
EDIT2: Here is my view:
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)