Reputation: 9146
I have the models:
class Project(models.Model):
project_collaborators = models.ManyToManyField(User)
class Node(models.Model):
collaborators = models.ManyToManyField(User)
project = models.ForeignKey(Project)
I would like to get all users that are node collaborators and also a project collaborator, assuming I have the pk of the node. What query would I need for that?
Upvotes: 2
Views: 72
Reputation: 239380
from django.db.models import F
User.objects.filter(node__pk=node.pk, project=F('node__project'))
That should work. Haven't tested it. If not, you can try this one as well:
User.objects.filter(node__pk=node.pk, project__node__pk=node.pk)
Assuming they both work, you might want to try both in something like django-debug-toolbar's debugsqlshell
management command to see what kind of query each produces and which might be more efficient.
Upvotes: 1