模型查询后,我试图访问ManyToManyFireld属性,但返回None
。
这是模型的样子。
class Role(models.Model):
ROLES = Choices('user', 'staff', 'admin')
user = AutoOneToOneField(
'account.User',
related_name='_role',
primary_key=True,
on_delete=models.CASCADE
)
role = models.CharField(max_length=10, choices=ROLES, default=ROLES.user)
locations = models.ManyToManyField('location.Location', related_name='_role', blank=True)
def __str__(self):
return '<Role: user={0} ({1})>'.format(self.user_id, self.role)
当我在“角色”查找上调用位置时,我会返回location.Location.None
,这就是查询查找的样子。
user_role = Role.objects.get(pk='123249882323')
role_locations = user_role.locations
print(role_locations) => location.Location.None
我希望role_locations
返回与Role
关联的所有位置