[Answered ]-Django: how to change status of cart item, if its quantity more than product's general_quantity?

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 don’t require Model to be added as suffix, so it is better to name the models as CartItem, Cart and Product from CartItemModel, CartModel and ProductModel respectively.

Leave a comment