Within my Django models I have created a decimal field like this:
price = models.DecimalField(_(u\'Price\'), decimal_places=2, max_digits=12)
O
Assuming this is your product model, and you want to add non-negative constraint on price field. You can add the meta constraint on the model:
class Product(models.Model):
price = models.DecimalField(max_digits=13, decimal_places=2)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
managed = True
db_table = 'product'
constraints = [
models.CheckConstraint(check=models.Q(price__gte='0'), name='product_price_non_negative'),
]