views:

1256

answers:

1

Given a situation in Django 1.0 where you have extra data on a Many-to-Many relationship:

class Player(models.Model):
  name = models.CharField(max_length=80)

class Team(models.Model):
  name = models.CharField(max_length=40)
  players = models.ManyToManyField(Player, through='TeamPlayer', related_name='teams')

class TeamPlayer(models.Model):
  player = models.ForeignKey(Player)
  team = models.ForeignKey(Team)
  captain = models.BooleanField()

The many-to-many relationship allows you to access the related data using attributes (the "players" attribute on the Team object or using the "teams" attribute on the Player object by way of its related name). When one of the objects is placed into a context for a template (e.g. a Team placed into a Context for rendering a template that generates the Team's roster), the related objects can be accessed (i.e. the players on the teams), but how can the extra data (e.g. 'captain') be accessed along with the related objects from the object in context (e.g.the Team) without adding additional data into the context?

I know it is possible to query directly against the intermediary table to get the extra data. For example:

TeamPlayer.objects.get(player=790, team=168).captain

Or:

for x in TeamPlayer.objects.filter(team=168):
  if x.captain:
    print "%s (Captain)" % (x.player.name)
  else:
    print x.player.name

Doing this directly on the intermediary table, however requires me to place additional data in a template's context (the result of the query on TeamPlayer) which I am trying to avoid if such a thing is possible.

+2  A: 

So, 15 minutes after asking the question, and I found my own answer.

Using dir(Team), I can see another generated attribute named teamplayer_set (it also exists on Player).

t = Team.objects.get(pk=168)
for x in t.teamplayer_set.all():
  if x.captain:
    print "%s (Captain)" % (x.player.name)
  else:
    print x.player.name

Not sure how I would customize that generated related_name, but at least I know I can get to the data from the template without adding additional query results into the context.

iammichael
If you do this way, you'll query the DB each time x.play.name is executed(so that the Player is grabbed from DB). Look up the docs for selected_related.
Tiago
Yup. I'm still learning django (and python), but I'm aware of select_related(). I left it out for the simplicity of the example.
iammichael
I'm in the exact same situation (django noob with ManyToMany situations) and I was wondering if there's a good way to display this data under a template.For example, if I wanted to display every exiting team and under every team display just the captain. Do I have to cycle through every team first and then through every teamplayer_set entry?
Edmundito
I suspect the answer to that is "yes" since you only have the teamplayer_set available to you in the context (and thus you must iterate over it to get to the data you want). But in the case you're talking about, it probably makes more sense to add more data into the context -- something like TeamPlayer.objects.filter(captain=true)
iammichael