2012-01-01 127 views
0

我想在从另一个模型链接到的模型的管理员屏幕中显示一个字段。在Django Admin窗口中显示来自父表的字段

我有车型像这样:

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) 

和管理模式是这样的:

class BuildingAdmin(admin.ModelAdmin): 
    list_display = ('name', 'location') 

class RoomAdmin(admin.ModelAdmin): 
    list_display = ('name', 'building') 

我怎样才能显示在管理列表中的房间模型3列,房间名称,建筑物名称和位置名称?

感谢

+0

您的“管理屏幕”是什么意思?更改列表?改变形式? – 2012-01-01 19:14:02

+0

列表,因此房间模型条目列表的列将为Room,Building,Location。 – Rob 2012-01-02 05:01:07

回答

1

你可以写在管理员自定义方法和list_display使用它。

class PersonAdmin(admin.ModelAdmin): 
    list_display = ('upper_case_name',) 

    def upper_case_name(self, obj): 
     return ("%s %s" % (obj.first_name, obj.last_name)).upper() 
    upper_case_name.short_description = 'Name' 

这样你就可以在list_display中显示任何你想要的东西。 Here你有完整的文档。

+0

确实如此,但在这种情况下,第三列不是模型中的字段,它是模型中该模型通过外键链接到的字段。这可能吗? – Rob 2012-01-02 05:06:34

+0

是的,这是可能的。试试看。 类RoomAdmin(admin.ModelAdmin): – gruszczy 2012-01-02 11:28:47

+0

我问这个问题之前尝试过这种 \t list_display =( '名', '建筑', 'building.location') – Rob 2012-01-02 19:06:57

相关问题