1
0
mirror of https://github.com/django/django.git synced 2025-11-07 07:15:35 +00:00

Fixed #29981 -- Fixed inline formsets with a OnetoOneField primary key that uses to_field.

This commit is contained in:
Patrik Sletmo
2018-12-11 20:41:21 +01:00
committed by Tim Graham
parent 02c07be95c
commit 14e2b1b065
3 changed files with 25 additions and 4 deletions

View File

@@ -958,8 +958,12 @@ class BaseInlineFormSet(BaseModelFormSet):
kwargs = {
'label': getattr(form.fields.get(name), 'label', capfirst(self.fk.verbose_name))
}
if self.fk.remote_field.field_name != self.fk.remote_field.model._meta.pk.name:
kwargs['to_field'] = self.fk.remote_field.field_name
# The InlineForeignKeyField assumes that the foreign key relation is
# based on the parent model's pk. If this isn't the case, set to_field
# to correctly resolve the initial form value.
if self.fk.remote_field.field_name != self.fk.remote_field.model._meta.pk.name:
kwargs['to_field'] = self.fk.remote_field.field_name
# If we're adding a new object, ignore a parent's auto-generated key
# as it will be regenerated on the save request.