Opened 16 years ago

Closed 16 years ago

Last modified 13 years ago

#8841 closed (fixed)

ModelChoiceIterator does not work with multi table inheritance

Reported by: hmarr Owned by: nobody
Component: Forms Version: dev
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: no UI/UX: no

Description

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.

Example:

<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:
            try:
                key = getattr(obj, self.field.to_field_name).pk
            except AttributeError:
                key = getattr(obj, self.field.to_field_name)
        else:
            key = obj.pk
        return (key, self.field.label_from_instance(obj))

Change History (5)

comment:1 by Michael Radziej, 16 years ago

Component: Admin interfaceForms
Needs tests: set
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 by hmarr, 16 years ago

Cc: hmarr added

comment:3 by flosch, 16 years ago

Cc: flosch added

comment:4 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: newclosed

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

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 by Jacob, 13 years ago

milestone: 1.0

Milestone 1.0 deleted

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