[Django]-Invalid embedded document instance provided to an EmbeddedDocumentField

0👍

I was able to make it work by changing the Bill Serializer as below

class BillSerializer(DocumentSerializer):
    orders = OrderSerializer(many = True)

    class Meta:
        model = Bill
        fields = '__all__'
        read_only_fields = ('id',)

    def create(self, validated_data):
        orders = validated_data.pop('orders')
        bill = Bill.objects.create(**validated_data)
        bill.orders = []    

        for order in orders:
            bill.orders.append(Order(**order))


        bill.save()

        return bill

0👍

Change your OrderSerializer

class OrderSerializer(EmbeddedDocumentSerializer):
   class Meta:
      model = Order
      fields = '__all__'
      read_only_fields = ('id',)

Leave a comment