I'm trying to create nested objects in Django Rest Framework according to the docs.
this my models.py :
from django.db import models
class Request(models.Model):
origin = models.CharField(max_length=255, blank=True)
def __str__(self):
return str(self.id)
class RequestOrders(models.Model):
request_id = models.ForeignKey(Request, related_name='request_orders', on_delete=models.DO_NOTHING, blank=True, null=True)
position = models.CharField(max_length=255, blank=True)
destination = models.CharField(max_length=255, blank=True)
order_ref = models.CharField(max_length=255, blank=True)
def __str__(self):
return self.id
class RequestOrderItemss(models.Model):
request_order_id = models.ForeignKey(RequestOrders, related_name='request_order_itemss', on_delete=models.DO_NOTHING, blank=True, null=True)
product_id = models.ForeignKey('product.Product', on_delete=models.DO_NOTHING, blank=True, null=True)
qty = models.CharField(max_length=255, blank=True)
def __str__(self):
return self.id
Here's my serializers.py :
from rest_framework import serializers
from request.models import Request, RequestOrders, RequestOrderItemss
from product.serializers import ProductSerializer
class RequestOrderItemssSerializer(serializers.ModelSerializer):
class Meta:
model = RequestOrderItemss
fields = ('id', 'request_order_id', 'product_id', 'qty')
class RequestOrdersSerializer(serializers.ModelSerializer):
request_order_itemss = RequestOrderItemssSerializer(many=True)
class Meta:
model = RequestOrders
fields = ('id', 'request_id', 'position', 'destination', 'order_ref', 'request_order_itemss')
class RequestSerializer(serializers.ModelSerializer):
request_orders = RequestOrdersSerializer(many=True)
class Meta:
model = Request
fields = ('id', 'origin', 'request_orders')
def create(self, validated_data):
request_orders_data = validated_data.pop('request_orders')
request = Request.objects.create(**validated_data)
for request_order_data in request_orders_data:
request_order_items_data = request_order_data.pop('request_order_itemss')
request_order = RequestOrders.objects.create(request_id=request,
**request_order_data)
for request_order_item_data in request_order_items_data:
RequestOrderItemss.objects.create(request_order_id=request_order,
**request_order_item_data)
return request
def update(self, instance, validated_data):
request_orders_data = validated_data.pop('request_orders')
orders = instance.request_orders.all()
orders = list(orders)
instance.origin = validated_data.get('origin', instance.origin)
instance.save()
for request_order_data in request_orders_data:
request_order_items_data = request_order_data.pop('request_order_itemss')
items = instance.request_orders.get().request_order_itemss.all()
items = list(items)
for request_order_item_data in request_order_items_data:
item = items.pop(0)
item.product_id = request_order_item_data.get('product_id', item.product_id)
item.qty = request_order_item_data.get('qty', item.qty)
item.save()
order = orders.pop(0)
order.position = request_order_data.get('position', order.position)
order.destination = request_order_data.get('destination', order.destination)
order.order_ref = request_order_data.get('order_ref', order.order_ref)
order.save()
return instance
and this is my views.py
from django.http import JsonResponse
from rest_framework import generics
from rest_framework.permissions import IsAuthenticated
from request.models import Request, RequestOrders, RequestOrderItemss
from request.serializers import RequestSerializer, RequestOrdersSerializer, \
RequestOrderItemssSerializer
class RequestListView(generics.ListCreateAPIView):
queryset = Request.objects.all()
serializer_class = RequestSerializer
permission_classes = (IsAuthenticated,)
class RequestView(generics.RetrieveUpdateDestroyAPIView):
serializer_class = RequestSerializer
queryset = Request.objects.all()
permission_classes = (IsAuthenticated,)
class RequestOrdersListView(generics.ListCreateAPIView):
queryset = RequestOrders.objects.all()
serializer_class = RequestOrdersSerializer
permission_classes = (IsAuthenticated,)
class RequestOrdersView(generics.RetrieveUpdateDestroyAPIView):
serializer_class = RequestOrdersSerializer
queryset = RequestOrders.objects.all()
permission_classes = (IsAuthenticated,)
class RequestOrderItemssListView(generics.ListCreateAPIView):
queryset = RequestOrderItemss.objects.all()
serializer_class = RequestOrderItemssSerializer
permission_classes = (IsAuthenticated,)
class RequestOrderItemssView(generics.RetrieveUpdateDestroyAPIView):
serializer_class = RequestOrderItemssSerializer
queryset = RequestOrderItemss.objects.all()
permission_classes = (IsAuthenticated,)
this is my JSON data that i want to save in data base :
{
"origin": "Krishna Rungta",
"request_orders": [
{
"position":"2",
"destination" :"Paris",
"order_ref":"ref#123",
"request_order_itemss":[
{
"product_id":4,
"qty":444,
}
{
"product_id":5,
"qty":33,
}
]
}
{
"position":"3",
"destination" :"Paris",
"order_ref":"ref#124",
"request_order_itemss":[
{
"product_id":6,
"qty":123,
}
{
"product_id":4,
"qty":54,
}
]
}
]
}
when I try to test this on Postman I get this error :
Direct assignment to the reverse side of a related set is prohibited. Use request_order_itemss.set() instead.
So I want to know where is the problem and is what i did is right or is there something wrong.
LoadingRequest,loading_request_order_itemss, etc. That said, I would guess that the problem you are seeing is a result of the fact that you are not actually ever calling the nested create method (inRequestSerializer, you just createRequestOrdersobjects without deserializing the validated data first).LoadingRequest.objects.createinRequestSerializer, and you also have not provided an actual stack trace as I suggested, but I think I have a handle on the problem and will answer as well as I can