我有这些模型:
class Permission(models.Model):
name = models.CharField(u'Name', max_length=100)
codename = models.CharField(u'Code name', max_length=100)
class Role(models.Model):
name = models.CharField(u'Name', max_length=80)
organization = models.ForeignKey(Organization)
permissions = models.ManyToManyField(Permission, blank=True)
class UserProfileManager(models.Manager):
def get_query_set(self):
return super(UserProfileManager, self).get_query_set().select_related(
'user', 'organization'
)
class UserProfile(models.Model):
user = models.OneToOneField(User)
organization = models.ForeignKey(Organization, blank=True)
roles = models.ManyToManyField(Role, blank=True)
permissions = models.ManyToManyField(Permission, blank=True)
objects = UserProfileManager()
我想读取最多 2 个 SELECTS 的所有用户权限('permissions' 和 'roles__permissions')。
如果我尝试添加prefetch_related('permissions', 'roles__permissions')
到 UserProfileManager 我得到 3 个选择(用于权限、角色和角色__权限)
怎么做?