Skip to content
Advertisement

Checking properties of an object based on separete view (Django|Django-Rest-Framework)

I am using django-rest-framework and React.js. I need a seperate function in the backend to check if CartItem.amount is lower then ProductStock.quantity, if CartItem.product equals ProductStock.id of course (this part does not work). For the same function I need to check if the pricing is the same as in Product model, but suprisingly this part of a function works. What can I do to make sure CartItem.amount will get lowered if it is higher than ProductStock.quantity?

Code below:

Product is a blueprint for the rest of the models.

ProductStock tracks the amount of different sizes of all of products.

CartItem is a model used for tracking how many products a user bought.

models.py

JavaScript

views.py

JavaScript

Advertisement

Answer

You need to change indentation of JsonResponse cause it ends after first stock in product_stock. Also item.save() and more probably need indentation change. Revise that in clean Python.

JavaScript

You should think of rebuilding model tree, cause ProductStock with ForeignKey seems bad solution. It would be better to count in function of Product, something similar to:

JavaScript

because it works dynamically and you don’t need extra objects in database.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement