[英]How to get objects if user is in a field of another django model's many-to-many field?
I have the models: 我有模特:
class Project(models.Model):
title = models.CharField(max_length=75)
description = models.CharField(max_length=250)
...
class Node(models.Model):
title = models.CharField(max_length=75)
collaborators = models.ManyToManyField(User)
project = models.ForeignKey(Project)
What I'm trying to do is get all projects where the user requesting the page is part of a node within the project. 我要做的是获取请求页面的用户所在的所有项目是项目中节点的一部分。
For example: If there are projects A, B, C, D and the requesting user is a collaborator on nodes within projects A and D, projects A and D would be returned (preferably with the ability to access those nodes as well). 例如:如果项目A,B,C,D和请求用户是项目A和D中的节点上的协作者,则将返回项目A和D(最好还能够访问这些节点)。
What is the most efficient way of doing this? 这样做最有效的方法是什么?
Project.objects.filter(node__collaborators=my_user)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.