I am working on Restaurant Ordering Application. Order of items will be created as an Array of JSON object will be POSTed to orderdetail models, but when the stock of any of the item is not sufficient it will raise Exception. But I only can give an error for one item not all the items.
For example:
Current Stock
Apple 5pcs
Mango 10pcs
When I make an order of Apple 10pcs and Mango 20pcs. I want to get an Error Message saying that "Apple and Mango stock is not sufficient". But currently, I only get "Apple stock is not sufficient" because I put apple as the first object in the array. If I put mango as the first object, I will get "Mango stock is not sufficient".
For full of the code, you can check the repo link: here. My Models:
class Menu(models.Model):
image = models.ImageField(upload_to=path_and_rename)
name = models.CharField(max_length=100)
price = models.IntegerField()
category = models.IntegerField()
availability = models.BooleanField(default=False)
qty = models.IntegerField(default=100)
sellerID = models.ForeignKey(Seller, on_delete=models.PROTECT)
class OrderDetail(models.Model):
orderID = models.ForeignKey(Order, on_delete=models.PROTECT)
menuID = models.ForeignKey(Menu, on_delete=models.PROTECT)
price = models.IntegerField()
qty = models.IntegerField()
tableNumber = models.IntegerField()
done = models.BooleanField(default=False)
# orderTime = models.DateTimeField(auto_now_add=True)
# finishTime = models.DateTimeField(auto_now=True)
finishTime = models.DateTimeField(null=True, blank=True)
sellerID = models.ForeignKey(Seller, on_delete=models.PROTECT)
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
if self.done:
self.finishTime = datetime.datetime.now()
else:
menuID = self.menuID.id
menuObject = Menu.objects.get(id=menuID)
tempQty = menuObject.qty - self.qty
if tempQty>=0:
menuObject.qty = tempQty
menuObject.save()
else:
# return serializers.ValidationError()
raise serializers.ValidationError(menuObject.name + ": STOCK IS NOT SUFFICIENT")
super().save(force_insert, force_update, using, update_fields)
My View:
class OrderDetailViewset(viewsets.ModelViewSet):
serializer_class = serializers.OrderDetailSerializer
def get_queryset(self):
queryset = models.OrderDetail.objects.all()
sellerID = self.request.query_params.get('sellerID', None)
done = self.request.query_params.get('done', None)
if sellerID is not None:
queryset = queryset.filter(sellerID=sellerID)
if done is not None:
queryset = queryset.filter(sellerID=sellerID, done=done)
return queryset
# Enable Post of List
# https://stackoverflow.com/questions/37329771/django-rest-bulk-post-post-array-of-json-objects
# Accessed on March 9, 2019
def create(self, request, pk=None, company_pk=None, project_pk=None):
is_many = True if isinstance(request.data, list) else False
serializer = self.get_serializer(data=request.data, many=is_many)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
I think the problem is located where I raised the Exception, but I have no Idea how to fix it. Thank you before!
validate_{field_name}method. This way it will collect all field errors before raising an exception and you will get all errorsself.context.request.methodin the serializer to determine the HTTP method and do the appropriate check.