Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#1649 closed defect (worksforme)

Attempting to use foreignkey fields in repr definitions does not work in admin

Reported by: anonymous Owned by: Adrian Holovaty
Component: contrib.admin Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Let's assume the following models:

class Foo(meta.Model):
    pass

class Bar(meta.model):
    def __repr__(self):
        return "%s (%s)" % (self.id, self.foo.id)
    foo = meta.ForeignKey(Foo)

This generally raises an AttributeError when django admin attempts to display the models. It seems that the attribute foo is not loaded at that time.

Is there some special switch I need to pull, or some special self._ensure_all_dependencies_are_loaded() to call at the repr function?

Change History (2)

comment:1 by James Bennett, 19 years ago

To fetch a related object for this sort of use, do 'self.get_foo()' instead of 'self.foo'.

Also, general questions about how Django works should be sent to the django-users mailing list (http://groups.google.com/group/django-users/).

comment:2 by Adrian Holovaty, 19 years ago

Resolution: worksforme
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top