1
0
mirror of https://github.com/django/django.git synced 2025-10-25 06:36:07 +00:00

Fixed #24377 -- Fixed model inline formsets with primary key's that have defaults.

This commit is contained in:
Tim Graham
2015-02-20 14:28:34 -05:00
parent 00fbd8fd52
commit 1306cd1e8a
3 changed files with 54 additions and 1 deletions

View File

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