How do I run a query in django that selects all the projects in which I am a team member?
I have a command concept in my django application.
class Team(models.Model):
name = models.CharField(max_length=200)
#snip
team_members = models.ManyToManyField(User)
I would like to get all the commands the current user belongs to. Something along the lines
Team.objects.all().filter(request.user.id__in = team_members.all())
This obvious doesn't work. Does anyone have any suggestions on how to make such a query without going directly to sql? I looked through the django documentation from "in" requests, but I couldn't find my use case here.
Many thanks! Nick.
0
a source to share
1 answer
You don't need to in
here, Django handles this automatically in a ManyToMany lookup.
Also, you need to understand that the database fields should always be to the left of the search, since they are actually treated as parameters to the function.
What you really want is very simple:
Team.objects.filter(team_members=request.user)
or
request.user.team_set.all()
+4
a source to share