Code

Opened 6 years ago

Closed 6 years ago

Last modified 3 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: mremolt, hmarr, flosch Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:

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))

Attachments (0)

Change History (5)

comment:1 Changed 6 years ago by mir

  • Component changed from Admin interface to Forms
  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 6 years ago by hmarr

  • Cc hmarr added

comment:3 Changed 6 years ago by flosch

  • Cc flosch added

comment:4 Changed 6 years ago by mtredinnick

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

(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 Changed 3 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

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.