1π
β
Here is what you can do:
-
You have a M2M relation between
Team
andPlayer
, create it.class Player(models.Model): name = models.CharField(...) teams = models.ManyToManyField(Team, through='TeamHasPlayer')
-
Get your
Team
queryset and using the reverse relationprefetch
theteam.players
teams = Team.objects.all().prefetch_related('player_set')
-
Now create your team dict:
team_dict = {} for team in teams: team_dict[team.name] = list(team.player_set.all())
π€Todor
0π
I would do this in this way, which is more readable and maintainable.
dic_ = {}
for team in Team.objects.all():
dic_[team.name] = team.teamhasplayer_set.values_list('player', flat=True)
π€doniyor
- Django templatetags for jquery to get item from html templatetag
- How to search paragraph(s) for objects
- Python: return int(value) ValueError: invalid literal for int() with base 10: ''
- MySQL On Update not triggering for Django/TastyPie REST API
- Django template forloop
Source:stackexchange.com