Comment (by Herman S):
This needs attention.
I have the following models:
* Applicant
* Application, which has a field "applicant =
models.ForeignKey(Applicant)"
In ApplicationAdmin I'm trying to request the applicants name, phone,
email etc. with:[[BR]]
list_display = ['status', 'interview' ... 'applicant__name',
'applicant__email']
As the parent/child relationship is reversed from what Django's
ModelAdmin.inlines expect, there seems to not be a solution for my
problem.
--
Ticket URL: <http://code.djangoproject.com/ticket/5863#comment:27>
* status: reopened => closed
* resolution: => wontfix
Comment:
Replying to [comment:27 Herman S]:
>
> As the parent/child relationship is reversed from what Django's
ModelAdmin.inlines expect, there seems to not be a solution for my
problem.
There's a trivially-easy solution you can code: methods on your model or
model admin that return the information you desire, listed in your
list_display.
I'm closing this wontfix; having it be open seems to be confusing people
into thinking the requested function of allowing a specific field from a
related model to be shown in list_display is not possible. It is possible,
quite easily, with a feature (callables in list_display) added since this
ticket was originally opened. That feature is more powerful than simply
allowing traversal to a related model's field, so the syntax of how to do
it is not exactly the same as it would be if just that one piece was
implemented. But given the more powerful thing has been implemented, I
don't believe the addition of another way of doing the same thing is
necessary or worth the implementation/maintenance effort.
--
Ticket URL: <http://code.djangoproject.com/ticket/5863#comment:28>
Comment (by pihentagy):
And what about sorting?
--
Ticket URL: <http://code.djangoproject.com/ticket/5863#comment:29>
Comment (by kmtracey):
Replying to [comment:29 pihentagy]:
> And what about sorting?
Sorting is supported for callables in in list_display, as noted in
http://code.djangoproject.com/ticket/5863#comment:23
--
Ticket URL: <http://code.djangoproject.com/ticket/5863#comment:30>
* cc: brillgen (added)
* type: => Uncategorized
* severity: => Normal
Comment:
@kmtracey:
We would have to use over a 100 extra boilerplate functions at last
estimate since callables is being stated as the only supported means of
getting direct fields from foreign keys...some of them are simply a text
field that needs to be displayed as such..I have 2 questions for the
community:
1. Isn't there a performance impact of using a callable (select_related)
benefit loss which could happen in case of simple FK fields required?
If there is no impact, its irrelevant because the boiler plate code is not
thaat much trouble
However, if there is (and I believe there would be),
2. Would the django admin accept a patch as long as it met the
requirements of test cases, documentation etc etc.
I ask because there would be nothing more frustrating than developing a
fully featured patch against trunk just to hear that it'll never get
accepted due to contrary design ideas :(
--
Ticket URL: <http://code.djangoproject.com/ticket/5863#comment:31>
Comment (by lukeplant):
@brillgen:
It's difficult to believe there would be much difference between the two
methods if you are using 'select_related' correctly (and we don't want to
complicate the logic for select_related any further).
Also, note that you could easily write a function that would eliminate the
boilerplate, if it is really boilerplate that conforms to a common
pattern. Untested code:
{{{
#!python
def foreign_field(field_name):
def accessor(obj):
val = obj
for part in field_name.split('__'):
val = getattr(val, part)
return val
accessor.__name__ = field_name
return accessor
ff = foreign_field
class MyAdmin(ModelAdmin):
list_display = [ff('foreign_key__related_fieldname1'),
ff('foreign_key__related_fieldname2')]
}}}
--
Ticket URL: <http://code.djangoproject.com/ticket/5863#comment:32>
* easy: => 0
Comment:
@lukeplant: In the absence of any other solution your function is a
lifesaver. I guess I'm not pythonic enough yet to have thought of it on my
own...
Still it does have a few problems that can't be fixed...For instance, how
to assign the short_description attribute to be the same as what has
already been defined for the field? It can't be done the way I see it,
without explicitly passing the model involved or the short description
(which will have to be repeated here)
--
Ticket URL: <http://code.djangoproject.com/ticket/5863#comment:33>
Comment (by andybak):
I agree with @brillgen here.
It would be clearer and much more consistant to support the parent__child
syntax for accessing related models wherever feasible.
I believe it's already been added for admin list_filter.
--
Ticket URL: <http://code.djangoproject.com/ticket/5863#comment:34>