summaryrefslogtreecommitdiff
path: root/django/forms
diff options
context:
space:
mode:
Diffstat (limited to 'django/forms')
-rw-r--r--django/forms/models.py10
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