试图从数据库查询访问ManyToManyField,但返回None

时间:2019-06-18 22:37:42

标签: python django django-models django-rest-framework many-to-many

模型查询后,我试图访问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关联的所有位置

0 个答案:

没有答案