diff options
| author | Tim Graham <timograham@gmail.com> | 2015-02-20 14:28:34 -0500 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-02-23 08:44:27 -0500 |
| commit | 1306cd1e8acfb13602ee8dc40993b2505cd7523b (patch) | |
| tree | c73bb367f295e54403a24b27fe3878c80ef8756e /django/forms | |
| parent | 00fbd8fd527fd6ee0319361bf194cd51a1ac1bfb (diff) | |
Fixed #24377 -- Fixed model inline formsets with primary key's that have defaults.
Diffstat (limited to 'django/forms')
| -rw-r--r-- | django/forms/models.py | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/django/forms/models.py b/django/forms/models.py index 98f84a0a44..6889a3e5b4 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -800,7 +800,10 @@ class BaseModelFormSet(BaseFormSet): or (pk.rel and pk.rel.parent_link and pk_is_not_editable(pk.rel.to._meta.pk))) if pk_is_not_editable(pk) or pk.name not in form.fields: if form.is_bound: - pk_value = form.instance.pk + # If we're adding the related instance, ignore its primary key + # as it could be an auto-generated default which isn't actually + # in the database. + pk_value = None if form.instance._state.adding else form.instance.pk else: try: if index is not None: @@ -928,6 +931,11 @@ class BaseInlineFormSet(BaseModelFormSet): if self.fk.rel.field_name != self.fk.rel.to._meta.pk.name: kwargs['to_field'] = self.fk.rel.field_name + # If we're adding a new object, ignore a parent's auto-generated pk + # as it will be regenerated on the save request. + if self.instance._state.adding and form._meta.model._meta.pk.has_default(): + self.instance.pk = None + form.fields[name] = InlineForeignKeyField(self.instance, **kwargs) # Add the generated field to form._meta.fields if it's defined to make |
