1

我有三个模型项目,成员,用户

class Project
    members = ManyToManyField(User, through="Member")

class User
    #...user model...

class Member
    project = ForeignKey(Project)
    user = ForeignKey(User)
    isAdmin = BooleanField(default=False)

如果我想得到一个项目(p)的所有用户对象,我可以做 p.members。但是如何轻松获取项目的所有管理员(用户对象)?

Member.filter(project=p, isAdmin=True) # this gets all the admin member objects
# how to convert this to user objects?
4

1 回答 1

0
User.objects.filter(member__isAdmin=True, member__project=p)

Django docs -跨越关系的查找

于 2013-04-24T20:33:54.840 回答