1
0
mirror of https://github.com/django/django.git synced 2025-10-24 22:26:08 +00:00

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.
This commit is contained in:
Simon Charette
2016-03-24 20:04:25 -04:00
parent 2c125bded1
commit 1a403aa705
3 changed files with 26 additions and 3 deletions

View File

@@ -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)