1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #32210 -- Fixed model inlines with to_field that has a default.

This commit is contained in:
Neeraj Kumar
2023-06-07 01:27:32 +05:30
committed by Mariusz Felisiak
parent b91d62cca0
commit eed096574f
2 changed files with 29 additions and 1 deletions

View File

@@ -1177,7 +1177,13 @@ class BaseInlineFormSet(BaseModelFormSet):
to_field = self.instance._meta.get_field(kwargs["to_field"])
else:
to_field = self.instance._meta.pk
if to_field.has_default():
if to_field.has_default() and (
# Don't ignore a parent's auto-generated key if it's not the
# parent model's pk and form data is provided.
to_field.attname == self.fk.remote_field.model._meta.pk.name
or not form.data
):
setattr(self.instance, to_field.attname, None)
form.fields[name] = InlineForeignKeyField(self.instance, **kwargs)