Opened 8 years ago

Last modified 6 years ago

#27560 closed Bug

Formset.save for model with foreign key to concrete base model — at Initial Version

Reported by: Lorenzo Peña Owned by: nobody
Component: Forms Version: 1.9
Severity: Normal Keywords: formset inline_formset inline_formsetfactory
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

class A(models.Model):
  pass

class B(A):
  pass

class C(models.Model):
  some_model = models.ForeignKey(SomeModel)
  a_link = models.ForeignKey(A)

I'm using an inlineformset_factory where the parent model is SomeModel and the child model is C.
When calling formset.save() I'm getting an error from django/forms/models.py (line 910)

910:   pk_value = getattr(self.instance, self.fk.remote_field.field_name)
911:   setattr(obj, self.fk.get_attname(), getattr(pk_value, 'pk', pk_value))

The getattr call fails

if I wrap both lines in: if hasattr(self.instance, self.fk.remote_field.field_name):
it works good.

Not sure if the condition is the way to fix the bug or it's just preventing the original cause from happening.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top