[Fixed]-In Django, how do you retrieve data from extra fields on many-to-many relationships without an explicit query for it?

10๐Ÿ‘

โœ…

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

Leave a comment