如何在Django管理员的对象列表中显示外键字段?

时间:2010-03-12 21:03:01

标签: python django django-models django-admin

我有以下模型设置

class Player(models.Model):
   #slug = models.slugField(max_length=200)  
    Player_Name = models.CharField(max_length=100)
    Nick = models.CharField(max_length=100, blank=True)
   Jersy_Number = models.IntegerField()
   Team_id = models.ForeignKey('Team')    
   Postion_Choices = (
      ('M', 'Manager'),
      ('P', 'Player'),
  )
  Poistion =  models.CharField(max_length=1, blank=True, choices =Postion_Choices)  
  Red_card =  models.IntegerField( blank=True, null=True)
  Yellow_card =  models.IntegerField(blank=True, null=True)
  Points = models.IntegerField(blank=True, null=True)  
  #Pic = models.ImageField(upload_to=path/for/upload, height_field=height,        width_field=width, max_length=100)
class PlayerAdmin(admin.ModelAdmin):
   list_display = ('Player_Name',)
   search_fields = ['Player_Name',]

admin.site.register(Player, PlayerAdmin)


class Team(models.Model):
"""Model docstring"""
#slug = models.slugField(max_length=200)
Team_Name = models.CharField(max_length=100,)
College = models.CharField(max_length=100,)
Win = models.IntegerField(blank=True, null=True)
Loss  = models.IntegerField(blank=True, null=True)
Draw = models.IntegerField(blank=True, null=True)
#logo = models.ImageField(upload_to=path/for/upload, height_field=height, width_field=width, max_length=100)    
class Meta:
    pass

#def __unicode__(self):
  #   return Team_Name

#def save(self, force_insert=False, force_update=False):
  #  pass

@models.permalink
def get_absolute_url(self):
    return ('view_or_url_name')


class TeamAdmin(admin.ModelAdmin):
   list_display = ('Team_Name',)

   search_fields = ['Team_Name',]

admin.site.register(Team, TeamAdmin)

我的问题是如何访问管理网站以在添加播放器表单Team_name字段中显示Team_ID,目前它只在组合框中显示为Team object

3 个答案:

答案 0 :(得分:7)

你几乎就在那里,你已经评论过了,忘了正确地调用属性:

def __unicode__(self):
    return self.Team_Name

阅读documentation

答案 1 :(得分:5)

对于更新的开发人员(Python 3.x):

def __str__(self):
    return self.Team_name

答案 2 :(得分:1)

向团队对象添加unicode方法:

def __unicode__(self):
    return self.Team_name