Opened 8 years ago

Closed 8 years ago

Last modified 5 years ago

#8841 closed (fixed)

ModelChoiceIterator does not work with multi table inheritance

Reported by: hmarr Owned by: nobody
Component: Forms Version: master
Severity: Keywords:
Cc: Marc Remolt, hmarr, flosch Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:


The bug appears in the child admin interface view, when you use multi table inheritance.

A ForeignKey selectbox shows the output of __unicode__() from the parent object for select values instead of the PK.


<option value="Foo">Foo</option>

instead of

<option value="6">Foo</option>

We fixed it with the following code in django.forms.models.ModelChoiceIterator:

    def choice(self, obj): 
        if self.field.to_field_name:
                key = getattr(obj, self.field.to_field_name).pk
            except AttributeError:
                key = getattr(obj, self.field.to_field_name)
            key =
        return (key, self.field.label_from_instance(obj))

Change History (5)

comment:1 Changed 8 years ago by Michael Radziej

Component: Admin interfaceForms
Needs documentation: unset
Needs tests: set
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

I haven't verified the bug, but it looks like a real bug. I'm not sure if the fix is correct in case of a OneToOneField with a to_field different from the pk.

comment:2 Changed 8 years ago by hmarr

Cc: hmarr added

comment:3 Changed 8 years ago by flosch

Cc: flosch added

comment:4 Changed 8 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(In [8957]) Fixed #8841 -- Fixed a case of ForeignKeys to models constructed with

This patch is uglier than it needs to be (see comment in patch) to ensure no
accidental bug is introduced just before 1.0. We'll clean it up later.

comment:5 Changed 5 years ago by Jacob

milestone: 1.0

Milestone 1.0 deleted

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