Search code examples
djangodjango-rest-frameworkdjango-rest-viewsets

Why DRF Nested Serializer is returning an error


I am using django rest framework for my rest api based project. I am using nested serializer to save child model object along with parent object.

Below is snap of my code

This is my parent model :

class DeliveryNote(models.Model):
    vdate=models.DateField("Date")
    voucherno=models.CharField("Voucher No.")
class Meta:
    verbose_name="Delivery Note"
    verbose_name_plural="Delivery Notes"
    db_table="INV_DeliveryNote"

This is my child model :

Class DeliveryItem(models.Model):
    deliverynote=models.ForeignKey(DeliveryNote,on_delete=models.CASCADE,related_name='items')
    itemname=models.CharField("Item Name")
    qty=models.FloatField("Quantity")

        class Meta:
            verbose_name="Delivery Item"
            verbose_name_plural="Delivery Items"
            db_table="INV_DeliveryItems"

This is my parent serializer:

class DeliveryNoteSerializer(eserializer):
    items=DeliveryItemSerializer(required=False,many=True,read_only=False)
    def create(self, validated_data):
        items_data=validated_data.pop('items',None)
        vinstance=DeliveryNote.objects.create(**validated_data)
        if items_data:
            for item in items_data:
                DeliveryItem.objects.create(deliverynote=vinstance,**item)
        return vinstance

    def update(self,instance,validated_data):
        items_data=validated_data.pop('items',None)
        vinstance=super(DeliveryNoteSerializer, self).update(instance, validated_data)
        items_dict=dict((i.id,i) for i in instance.items.all())
        if items_data:
            for item in items_data:
                if "id" in item.keys():
                    iinstance=items_dict.pop(item["id"])
                    item.pop('id')
                    for x in item.keys():
                        setattr(iinstance,x,item[x])
                    iinstance.save()
                else:
                    DeliveryItem.objects.create(deliverynote=vinstance,**item)

        if len(items_dict)>0:
            for item in items_dict.values():
                item.delete()

        return vinstance

    class Meta:
        model = DeliveryNote
        fields='__all__'

This is my child serializer:

class DeliveryItemSerializer(serializers.ModelSerializer):
    def create(self, validated_data):
        return DeliveryItem.objects.create(**validated_data)
    class Meta:
        model = DeliveryItem
        fields='__all__'

I am using django rest framework viewset to generate end points.

My payload is:

{
    "vdate":"31-08-2020",
    "voucher_no":"111",
    "items":[
        {
            "itemname":12,
            "qty":"10.00"
        }
    ]
}

This generates following error :

"items": [
    {
        "deliverynote": [
            "This field is required."
        ]
    }
]

What am I doing wrong ? Please help.


Solution

  • In DeliveryNoteSerializer you are giving DeliveryItemSerializer, so in CreateMixin, in DelivertyItem, it is excepting foreign key(deliverynote), that's why it is throwing "deliverynote" is required.

    Try this

    class DeliveryItemSerializer(serializers.ModelSerializer):
        id = serializers.IntegerField(read_only=True)
    
        class Meta:
            model = DeliveryItem
            fields=(
                'id',
                'itemname',
                'qty'
            )
    
    class DeliveryNoteSerializer(serializers.ModelSerializer):
        items=DeliveryItemSerializer(required=False,many=True, read_only=False)
    
        class Meta:
            model = DeliveryNote
            fields = (
                'id',
                'vdate',
                'voucherno',
                'items',
            )
    
        def create(self, validated_data):
            items_data=validated_data.pop('items',None)
            vinstance=DeliveryNote.objects.create(**validated_data)
            if items_data:
                for item in items_data:
                    di = DeliveryItem.objects.create(deliverynote=vinstance,**item)
            return vinstance