diff options
| author | Tim Graham <timograham@gmail.com> | 2015-02-20 14:28:34 -0500 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-02-23 09:01:00 -0500 |
| commit | 41d5ed480cd55a71b739e779ca11f24caaa2b302 (patch) | |
| tree | 67d5d3df875fddefd3c73e68909426398b81a58c /django | |
| parent | 04c262aea9b398c79e753ae75aa12b1520e99854 (diff) | |
[1.8.x] Fixed #24377 -- Fixed model inline formsets with primary key's that have defaults.
Backport of 1306cd1e8acfb13602ee8dc40993b2505cd7523b from master
Diffstat (limited to 'django')
| -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 92906eb1fa..48fe2f4ac2 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -788,7 +788,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: @@ -915,6 +918,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 |
