Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Django, can you not use a string join on a ManyToManyField? Is ManyToMany not just a list?

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)
like image 256
rmh Avatar asked Feb 24 '09 15:02

rmh


1 Answers

Another option is:

return " vs ".join(p.full_name for p in self.players.all())

Note: Sorry for bringing this back -- gravedigger :-)

like image 150
jweyrich Avatar answered Dec 15 '22 21:12

jweyrich