我正在使用pinax-teams来建模团队和成员资格。尝试查看查找以下内容的最佳方法:
给定一个用户,获取他是其成员的所有团队。我目前有以下内容,效率太低。任何帮助,不胜感激。这是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
不需要如此复杂或低效的东西。您可以在单个查询中跟踪关系:
teams = Team.objects.filter(memberships__user=user)