I am using pinax-teams to model teams and memberships. Trying to see the best way to find the following:
Given a user, get all the teams he is the member of. I currently have the following and it is too inefficient. Any help is appreciated. Here is the link to pinax-teams https://github.com/pinax/pinax-teams/blob/master/pinax/teams/models.py
team_set = []
user_name = self.request.QUERY_PARAMS.get('user_name', None)
user = User()
if user_name is not None:
user = User.objects.get(username=user_name)
for team in Team.objects.all():
if team.for_user(user):
team_set.append(team)
return team_set
There's no need for anything so complex or inefficient. You can follow the relationships in a single query: