Code

Opened 5 years ago

Closed 5 years ago

#10331 closed (invalid)

Using model InlineFormset created with inlineformset_factory fails with KeyError

Reported by: Thomas Presthus <thomas@…> Owned by: nobody
Component: Forms Version: master
Severity: Keywords:
Cc: thomas@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The following code would fail with KeyError 'user' at django/forms/models.py in add_fields, line 507

from django.contrib.auth.models import User
from foo.models import InfoEntry

user = User.objects.get(pk=1)

InfoEntryFormset = inlineformset_factory(User, InfoEntry)
formset = InfoEntryFormset(instance=user)

It seems BaseInlineFormSet tries to access form.fields[self.fk.name] before it is created.
Attached is a patch against r9885 fixing this, but should probably be better investigated.

Attachments (1)

r9885-inlinemodelformset.diff (621 bytes) - added by Thomas Presthus <thomas@…> 5 years ago.
Patch for ticket #10331

Download all attachments as: .zip

Change History (3)

Changed 5 years ago by Thomas Presthus <thomas@…>

Patch for ticket #10331

comment:1 Changed 5 years ago by Thomas Presthus <thomas@…>

  • Cc thomas@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 5 years ago by brosner

  • Resolution set to invalid
  • Status changed from new to closed

I would suspect the foreign key to User is marked as editable=False which is incorrect. That would explain why form.fields does not contain that key. Marking invalid. Reopen if this is not the case.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.