Pinax teams - given a user find all the teams for which the user is a member

163 views Asked by At

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
1

There are 1 answers

0
Daniel Roseman On BEST ANSWER

There's no need for anything so complex or inefficient. You can follow the relationships in a single query:

teams = Team.objects.filter(memberships__user=user)