diff options
| author | Simon Charette <charette.s@gmail.com> | 2016-03-24 20:04:25 -0400 |
|---|---|---|
| committer | Simon Charette <charette.s@gmail.com> | 2016-03-26 21:49:41 -0400 |
| commit | 1a403aa705e7921eb6ec88c7d9f686ab0e54ef76 (patch) | |
| tree | 0a4d0cdab524eed7386001bf82da1a325b311417 /django/forms | |
| parent | 2c125bded1834cadf3a6132d9ab87bc74f5ed728 (diff) | |
Fixed #25987 -- Made inline formset validation respect unique_together with an unsaved parent object.
Thanks Anton Kuzmichev for the report and Tim for the review.
Diffstat (limited to 'django/forms')
| -rw-r--r-- | django/forms/models.py | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/django/forms/models.py b/django/forms/models.py index d0bdbde0b6..3cafdebd34 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -564,6 +564,9 @@ class BaseModelFormSet(BaseFormSet): """ model = None + # Set of fields that must be unique among forms of this set. + unique_fields = set() + def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, queryset=None, **kwargs): self.queryset = queryset @@ -677,9 +680,11 @@ class BaseModelFormSet(BaseFormSet): for uclass, unique_check in all_unique_checks: seen_data = set() for form in valid_forms: - # get data for each field of each of unique_check - row_data = (form.cleaned_data[field] - for field in unique_check if field in form.cleaned_data) + # Get the data for the set of fields that must be unique among the forms. + row_data = ( + field if field in self.unique_fields else form.cleaned_data[field] + for field in unique_check if field in form.cleaned_data + ) # Reduce Model instances to their primary key values row_data = tuple(d._get_pk_val() if hasattr(d, '_get_pk_val') else d for d in row_data) @@ -878,6 +883,7 @@ class BaseInlineFormSet(BaseModelFormSet): qs = queryset.filter(**{self.fk.name: self.instance}) else: qs = queryset.none() + self.unique_fields = {self.fk.name} super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix, queryset=qs, **kwargs) |
