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',)
- [Django]-Django 2 upgrade lost filter_horizontal functionality
- [Django]-ForeignKey to 'self' in sub-application throws error on makemigrations in Django project
- [Django]-How to load fixtures.json with ManyToMany Relationships in Django
Source:stackexchange.com