mirror of https://github.com/django/django.git
Fixed #32210 -- Fixed model inlines with to_field that has a default.
This commit is contained in:
parent
b91d62cca0
commit
eed096574f
|
@ -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)
|
||||
|
|
|
@ -93,3 +93,25 @@ class InlineFormsetTests(TestCase):
|
|||
)
|
||||
formset = FormSet()
|
||||
self.assertIsNone(formset.forms[0].fields["parent"].initial)
|
||||
|
||||
def test_inlineformset_factory_nulls_default_pks_alternate_key_relation_data(self):
|
||||
"""
|
||||
If form data is provided, a parent's auto-generated alternate key is
|
||||
set.
|
||||
"""
|
||||
FormSet = inlineformset_factory(
|
||||
ParentWithUUIDAlternateKey, ChildRelatedViaAK, fields="__all__"
|
||||
)
|
||||
formset = FormSet(
|
||||
{
|
||||
"childrelatedviaak_set-TOTAL_FORMS": 3,
|
||||
"childrelatedviaak_set-INITIAL_FORMS": 0,
|
||||
"childrelatedviaak_set-MAX_NUM_FORMS": "",
|
||||
"childrelatedviaak_set-0-name": "Test",
|
||||
"childrelatedviaak_set-1-name": "",
|
||||
"childrelatedviaak_set-2-name": "",
|
||||
}
|
||||
)
|
||||
self.assertIs(formset.is_valid(), True)
|
||||
self.assertIsNotNone(formset.instance.uuid)
|
||||
self.assertEqual(formset.forms[0].instance.parent_id, formset.instance.uuid)
|
||||
|
|
Loading…
Reference in New Issue