Search code examples
djangodjango-rest-frameworkdjango-viewsdjango-serializer

cant update a nested serializer of two model with many to one relation


I created two model named office and phone that phone save the office phones (each office can have several phone number) for working with this two model I created a nested-serializer and two generic view one for post and get request and the other one for put delete and retrieve I don't have problem in post ,get ,delete ,retrieve but in case of update method can't update phone numbers here is the code :

office model and phone model:

class Office(models.Model):
    doctor=models.ForeignKey(Doctor,on_delete=models.CASCADE)
    title=models.CharField(max_length=100)
    address=models.TextField()
    location=models.FloatField()

    def __str__(self):
        return f"{self.title}({self.id})"


class OfficePhone(models.Model):
    office=models.ForeignKey(Office,related_name='phone',on_delete=models.CASCADE)
    phone=models.TextField()

phone and office serializer:

class OfficeSerialzier(serializers.ModelSerializer):
    phone = OfficePhoneSerializer(many=True)

    def create(self, validated_data):
        phones = validated_data.pop('phone')
        office = Office.objects.create(**validated_data)
        for data in phones:
            p = OfficePhone(phone=data['phone'], office=office)
            p.save()
        return office


    def update(self, instance, validated_data):
        phone_numbers=validated_data.pop('phone')
        return super().update(instance, validated_data)

    class Meta():
        model = Office
        fields = ['id', 'doctor', 'title', 'address', 'location', 'phone']
        lookup_field = 'doctor'

office generics view :

class OfficeDetail(generics.RetrieveUpdateDestroyAPIView):
    serializer_class=OfficeSerialzier
    queryset=Office.objects.all()


class officeListByDoctorID(generics.ListAPIView):
    serializer_class = OfficeSerialzier

    def get_queryset(self):
        return Office.objects.filter(doctor=self.kwargs['doctor'])

and the api is like bellow when i request put method cant get id of phones in serializer:

{
    "id": 25,
    "doctor": 2,
    "title": "new title updated",
    "address": "تهران",
    "location": 454454.0,
    "phone": [
        {
            "id": 33,
            "phone": "+914221036"
        },
        {
            "id": 34,
            "phone": "+9144562528"
        },
        {
            "id": 35,
            "phone": "+9144562528"
        }
    ]
}

Solution

  • for updating every time i deleted each phone numbers of office and rewrite it again

    class OfficeSerialzier(serializers.ModelSerializer):
        phone = OfficePhoneSerializer(many=True)
    
        def create(self, validated_data):
            phones = validated_data.pop('phone')
            office = Office.objects.create(**validated_data)
            for data in phones:
                p = OfficePhone(phone=data['phone'], office=office)
                p.save()
            return office
    
    
        def update(self, instance, validated_data):
            phone_numbers= validated_data.pop('phone')
            OfficePhone.objects.filter(office=instance.id).delete()
            for data in phone_numbers:
                OfficePhone.objects.create(office=instance,phone=data.get('phone'))
            return super().update(instance, validated_data)
    
        class Meta():
            model = Office
            fields = ['id', 'doctor', 'title', 'address', 'location', 'phone']
            lookup_field = 'doctor'