I have two models in my Django project:
- Match
- Player
Match has a ManyToMany property pointing at players, so that multiple players can compete in a match. I'd like to return an informative object name in the Django admin, something like "Richard Henry vs John Doe", by using a join on the players' full names. However the following fails:
class Match(models.Model):
players = models.ManyToManyField(Player, verbose_name='Competitors')
def __unicode__(self):
return " vs ".join(self.players)
Are ManyToManyFields not just lists? Why can't I join them? Any input is appreciated. Here's my player model, incase that helps:
class Player(models.Model):
full_name = models.CharField(max_length=30)
def __unicode__(self):
return "%s" % self.full_name
Thanks!
Edit: I just discovered that I can use self.players.list_display
to have this returned as a list. I'm no longer spat a traceback, but for some reason the __unicode__
name now returns None
. Any idea why that would be?
Edit 2: Changed code:
class Match(models.Model):
players = models.ManyToManyField(Player, verbose_name='Competitors')
def __unicode__(self):
return " vs ".join(self.players.list_display)