I want to show a field in the admin screen from a model that is linked to from another model.
I have models like so:
class Location(models.Model):
name = models.CharField(max_length = 128)
description = models.TextField(blank = True)
class Building(models.Model):
name = models.CharField(max_length = 128)
description = models.TextField(blank = True)
location = models.ForeignKey(Location)
class Room(models.Model):
name = models.CharField(max_length = 128)
description = models.TextField(blank = True)
building = models.ForeignKey(Building)
And admin models like this:
class BuildingAdmin(admin.ModelAdmin):
list_display = ('name', 'location')
class RoomAdmin(admin.ModelAdmin):
list_display = ('name', 'building')
How can I show in the admin list for the Room model 3 columns, room name, building name and location name?
Thanks
You can write a custom method in admin and use it in
list_display.This way you can display whatever you want in list_display. Here you have full docs.