I have two models Product and Cart. Product model has maximum_order_quantity
. While updating quantity in cart, I'll have to check whether quantity is greater than maximum_order_quantity
at database level. For that am comparing quantity with maximum_order_quantity
in Cart Model But it throws an error when I try to migrate
cart.CartItems: (models.E041) 'constraints' refers to the joined field 'product__maximum_order_quantity'
.
Below are my models
class Products(models.Model):
category = models.ForeignKey(
Category, on_delete=models.CASCADE, related_name="products"
)
product_name = models.CharField(max_length=50, unique=True)
base_price = models.IntegerField()
product_image = models.ImageField(
upload_to="photos/products", null=True, blank=True
)
stock = models.IntegerField(validators=[MinValueValidator(0)])
maximum_order_quantity = models.IntegerField(null=True, blank=True)
)
class CartItems(models.Model):
cart = models.ForeignKey(Cart, on_delete=models.CASCADE)
product = models.ForeignKey(Products, on_delete=models.CASCADE)
quantity = models.IntegerField()
class Meta:
verbose_name_plural = "Cart Items"
constraints = [
models.CheckConstraint(
check=models.Q(quantity__gt=models.F("product__maximum_order_quantity")),
name="Quantity cannot be more than maximum order quantity"
)
]
Error
SystemCheckError: System check identified some issues:
ERRORS:
cart.CartItems: (models.E041) 'constraints' refers to the joined field 'product__maximum_order_quantity'.