#26524: Django Admin list_display - Unable to display foreign key id
-------------------------------+-------------------------------------------
     Reporter:  cristianocca   |      Owner:  nobody
         Type:  Bug            |     Status:  new
    Component:  contrib.admin  |    Version:  1.9
     Severity:  Normal         |   Keywords:  admin list_display changelist
 Triage Stage:  Unreviewed     |  Has patch:  0
Easy pickings:  0              |      UI/UX:  0
-------------------------------+-------------------------------------------
 Given these two models.


 {{{
 class A(models.Model):
         name = models.CharField(max_length=100)

         def __unicode__(self):
                 return self.name

 class B(models.Model):
         name = models.CharField(max_length=100)
         fk = models.ForeignKey(A)

         def __unicode__(self):
                 return self.name
 }}}

 And these model admin


 {{{
 class AAdmin(admin.ModelAdmin):
     list_display = ('id','name')
 admin.site.register(A, AAdmin)

 class BAdmin(admin.ModelAdmin):
     list_display = ('id','name','fk_id','fk')
 admin.site.register(B, BAdmin)
 }}}

 As you see, for BAdmin I'm trying to display the actual id of the foreign
 key, so I prevent an additional unwanted join (or worse, one query per
 related object if no select related is added). However, the admin page
 refuses to display the id and instead renders the whole object (calling
 the __unicode__ method) which causes the additional join I don't want.
 In order to demostrate it I also added the 'fk' relation which works as
 expected

 The change list result is then


 {{{
 1       test B  test A  test A
 }}}

 where it should be

 {{{
 1       test B  1       test A
 }}}


 In order to work around it, I can define a callable that just returns the
 id property, but that's terrible because I lose any sort option on it plus
 I need to write quite a few more lines.

--
Ticket URL: <https://code.djangoproject.com/ticket/26524>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/055.17dfa47b739060b687961ed0cda5ee2f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to