#admin.py
class GameListAdmin(admin.ModelAdmin):
list_display = ['game', 'position']
ordering = ('position',)
class GameAdmin(admin.ModelAdmin):
list_display = ['name', 'status']
actions = [make_published]
#models.py
class Game(models.Model):
name = models.CharField(max_length=200)
status = models.CharField(max_length=1, choices=STATUS_CHOICES)
def __unicode__(self):
return self.name
class GameList(models.Model):
game = models.ForeignKey(Game)
position = models.IntegerField()
def __unicode__(self):
return self.game.name
我正在尝试在GameListAdmin的list_display中显示game.status但不是 确定如何使用Admin.py中的ForeignKey进行向后查找
有什么想法吗?
答案 0 :(得分:2)
你可以这样做:
def get_status(obj):
return '%s' % (obj.game.status)
get_status.short_description = 'Status'
class GameListAdmin(admin.ModelAdmin):
list_display = ['game', 'position', get_status]
请参阅docs。
答案 1 :(得分:2)
您可以随时定义自己的专栏,例如文档说明:http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display
例如:
class GameListAdmin(admin.ModelAdmin):
list_display = ['game_status', 'position']
ordering = ('position',)
def game_status(self, obj):
return obj.game.status
game_status.short_description = 'Game status'