summaryrefslogtreecommitdiff
path: root/django/forms
diff options
context:
space:
mode:
authorPatrik Sletmo <patrik.sletmo@gmail.com>2018-12-11 20:41:21 +0100
committerTim Graham <timograham@gmail.com>2019-01-01 10:40:22 -0500
commit14e2b1b065085c1d2d3e94ebaeefe25e12595a00 (patch)
tree48931fa81c56ba88cbf9c27058825244f3267b4d /django/forms
parent02c07be95c47efaab9da7422c33ee76142f11336 (diff)
Fixed #29981 -- Fixed inline formsets with a OnetoOneField primary key that uses to_field.
Diffstat (limited to 'django/forms')
-rw-r--r--django/forms/models.py8
1 files changed, 6 insertions, 2 deletions
diff --git a/django/forms/models.py b/django/forms/models.py
index 7648d97da4..fe8a67ed2b 100644
--- a/django/forms/models.py
+++ b/django/forms/models.py
@@ -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.