views:

291

answers:

2
#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

I'm trying to show game.status in GameListAdmin's list_display but not sure how to do a backwards lookup using the ForeignKey in Admin.py

Any ideas?

+2  A: 

You can do it like this:

def get_status(obj):
    return '%s' % (obj.game.status)
get_status.short_description = 'Status'

class GameListAdmin(admin.ModelAdmin):
    list_display = ['game', 'position', get_status]

See the docs.

Dominic Rodger
Brilliant, thanks.
Rob B
+2  A: 

You can always define your own column, like the doc explains : http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

For example :

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'
vincent