Opened 9 years ago
Last modified 8 years ago
#26524 closed Cleanup/optimization
Django Admin list_display - Unable to display foreign key id — at Initial Version
Reported by: | Cristiano Coelho | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Release blocker | Keywords: | admin list_display changelist |
Cc: | Cristiano Coelho, krishna.bmsce@…, zachborboa@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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.