1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #35676 -- Made BaseModelForm validate constraints that reference an InlineForeignKeyField.

Co-authored-by: Simon Charette <charette.s@gmail.com>
This commit is contained in:
Clifford Gama
2025-03-08 15:46:58 +02:00
committed by Sarah Boyce
parent 5183f7c287
commit 0ebea6e5c0
7 changed files with 135 additions and 13 deletions

View File

@@ -521,3 +521,24 @@ class Dice(models.Model):
through=NumbersToDice,
limit_choices_to=models.Q(value__gte=1),
)
class ConstraintsModel(models.Model):
name = models.CharField(max_length=100)
category = models.CharField(max_length=50, default="uncategorized")
price = models.DecimalField(max_digits=10, decimal_places=2, default=0)
class Meta:
constraints = [
models.UniqueConstraint(
"name",
"category",
name="unique_name_category",
violation_error_message="This product already exists.",
),
models.CheckConstraint(
condition=models.Q(price__gt=0),
name="price_gte_zero",
violation_error_message="Price must be greater than zero.",
),
]