summaryrefslogtreecommitdiff
path: root/django/forms
diff options
context:
space:
mode:
authorClifford Gama <cliffygamy@gmail.com>2025-03-08 15:46:58 +0200
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-03-12 09:16:15 +0100
commit0ebea6e5c07485a36862e9b6e2be18d1694ad2c5 (patch)
tree6ece342336bea4fbdd74cd29a51d9a071901fc82 /django/forms
parent5183f7c287a9a5d61ca1103b55166cda52d9c647 (diff)
Fixed #35676 -- Made BaseModelForm validate constraints that reference an InlineForeignKeyField.
Co-authored-by: Simon Charette <charette.s@gmail.com>
Diffstat (limited to 'django/forms')
-rw-r--r--django/forms/models.py28
1 files changed, 23 insertions, 5 deletions
diff --git a/django/forms/models.py b/django/forms/models.py
index d220e3c90f..9acd2d7280 100644
--- a/django/forms/models.py
+++ b/django/forms/models.py
@@ -370,10 +370,12 @@ class BaseModelForm(BaseForm, AltersData):
# if initial was provided, it should override the values from instance
if initial is not None:
object_data.update(initial)
- # self._validate_unique will be set to True by BaseModelForm.clean().
- # It is False by default so overriding self.clean() and failing to call
- # super will stop validate_unique from being called.
+ # self._validate_(unique|constraints) will be set to True by
+ # BaseModelForm.clean(). It is False by default so overriding
+ # self.clean() and failing to call super will stop
+ # validate_(unique|constraints) from being called.
self._validate_unique = False
+ self._validate_constraints = False
super().__init__(
data,
files,
@@ -436,6 +438,7 @@ class BaseModelForm(BaseForm, AltersData):
def clean(self):
self._validate_unique = True
+ self._validate_constraints = True
return self.cleaned_data
def _update_errors(self, errors):
@@ -495,13 +498,17 @@ class BaseModelForm(BaseForm, AltersData):
self._update_errors(e)
try:
- self.instance.full_clean(exclude=exclude, validate_unique=False)
+ self.instance.full_clean(
+ exclude=exclude, validate_unique=False, validate_constraints=False
+ )
except ValidationError as e:
self._update_errors(e)
- # Validate uniqueness if needed.
+ # Validate uniqueness and constraints if needed.
if self._validate_unique:
self.validate_unique()
+ if self._validate_constraints:
+ self.validate_constraints()
def validate_unique(self):
"""
@@ -514,6 +521,17 @@ class BaseModelForm(BaseForm, AltersData):
except ValidationError as e:
self._update_errors(e)
+ def validate_constraints(self):
+ """
+ Call the instance's validate_constraints() method and update the form's
+ validation errors if any were raised.
+ """
+ exclude = self._get_validation_exclusions()
+ try:
+ self.instance.validate_constraints(exclude=exclude)
+ except ValidationError as e:
+ self._update_errors(e)
+
def _save_m2m(self):
"""
Save the many-to-many fields and generic relations for this form.