1👍
✅
You are doing the object creations in a wrong order, that’s why you get IntegrityError: order_order.delivery_id may not be NULL
. Check your model structures again:
Your Order
models has ForeignKey's
to Delivery
and Payments
, which means that you need to create Delivery
and Payments
objects before Order
.
Try something like this:
def create(self, validated_data):
deliverys_data = validated_data.pop('delivery')
delivery = Delivery.objects.create(**deliverys_data)
paymentsMethod_data = validated_data.pop('paymentMethod')
payment_method = Payments.objects.create(**paymentsMethod_data)
order = Order.objects.create(
delivery=delivery,
paymentMethod=payment_method,
**validated_data
)
return order
Source:stackexchange.com