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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.