mirror of
https://github.com/django/django.git
synced 2024-12-24 10:05:46 +00:00
[4.2.x] Improved formset docs by using a set instead of a list in the custom validation example.
Backport of c59be9f1da
from main
This commit is contained in:
parent
f55b420277
commit
dcb9d7a0e4
@ -378,14 +378,14 @@ is where you define your own validation that works at the formset level:
|
|||||||
... if any(self.errors):
|
... if any(self.errors):
|
||||||
... # Don't bother validating the formset unless each form is valid on its own
|
... # Don't bother validating the formset unless each form is valid on its own
|
||||||
... return
|
... return
|
||||||
... titles = []
|
... titles = set()
|
||||||
... for form in self.forms:
|
... for form in self.forms:
|
||||||
... if self.can_delete and self._should_delete_form(form):
|
... if self.can_delete and self._should_delete_form(form):
|
||||||
... continue
|
... continue
|
||||||
... title = form.cleaned_data.get("title")
|
... title = form.cleaned_data.get("title")
|
||||||
... if title in titles:
|
... if title in titles:
|
||||||
... raise ValidationError("Articles in a set must have distinct titles.")
|
... raise ValidationError("Articles in a set must have distinct titles.")
|
||||||
... titles.append(title)
|
... titles.add(title)
|
||||||
...
|
...
|
||||||
|
|
||||||
>>> ArticleFormSet = formset_factory(ArticleForm, formset=BaseArticleFormSet)
|
>>> ArticleFormSet = formset_factory(ArticleForm, formset=BaseArticleFormSet)
|
||||||
|
Loading…
Reference in New Issue
Block a user