Django

Code

Ticket #8841 (closed: fixed)

Opened 3 months ago

Last modified 3 months ago

ModelChoiceIterator does not work with multi table inheritance

Reported by: hmarr Assigned to: nobody
Milestone: 1.0 Component: Forms
Version: SVN Keywords:
Cc: mremolt, hmarr, flosch Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 0

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

Change History

09/03/08 09:06:34 changed by mir

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • component changed from Admin interface to Forms.
  • needs_tests set to 1.
  • needs_docs changed.

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.

09/03/08 10:37:42 changed by hmarr

  • cc changed from mremolt to mremolt, hmarr.

09/03/08 11:44:19 changed by flosch

  • cc changed from mremolt, hmarr to mremolt, hmarr, flosch.

09/03/08 17:45:34 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(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.


Add/Change #8841 (ModelChoiceIterator does not work with multi table inheritance)




Change Properties
Action