Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a foreign key fields in the objects listing of the Django admin?

i have the following models setup

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)

my question is how do i get to the admin site to show Team_name in the add player form Team_ID field currently it is only showing up as Team object in the combo box

like image 803
nashr rafeeg Avatar asked Dec 08 '22 04:12

nashr rafeeg


1 Answers

You are almost there, you have commented it out and forgot to call the attribute properly:

def __unicode__(self):
    return self.Team_Name

Read the documentation.

like image 119
Felix Kling Avatar answered Jan 05 '23 04:01

Felix Kling