1👍
✅
You can modify your CartItemSerializer
to check the quantity and update the status in the validate()
method, like so:
class CartItemSerializer(serializers.ModelSerializer):
singe_item_price = serializers.SerializerMethodField('get_item_price')
def get_item_price(self, obj):
return obj.product.price
def validate(self, data):
"""
Check that quantity is not greater than general_quantity
"""
if data['quantity'] > data['product'].general_quantity:
data['status'] = 'n'
return data
class Meta:
model = CartItemModel
exclude = ('cart',)
Note: Models in django don’t require
Model
to be added as suffix, so it is better to name the models asCartItem
,Cart
andProduct
fromCartItemModel
,CartModel
andProductModel
respectively.
Source:stackexchange.com