django - 如何在自定义用户模型中使用 django-guardian

标签 django python-3.x django-guardian

我正在使用 django-guardian 来检查用户的对象权限。在我的特殊情况下,我扩展了用户模型。 在我的 models.py 中,我扩展了这样的用户模型: enter image description here

class User(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    active = models.BooleanField(default=True)
    staff = models.BooleanField(default=False) 
    admin = models.BooleanField(default=False) 

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = [] # Email & Password are required by default.

    def get_full_name(self):
        # The user is identified by their email address
        return self.email

    def get_short_name(self):
        # The user is identified by their email address
        return self.email

    def __str__(self):              # __unicode__ on Python 2
        return self.email

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        return self.staff

    @property
    def is_admin(self):
        "Is the user a admin member?"
        return self.admin

    @property
    def is_active(self):
        "Is the user active?"
        return self.active

在我的对象模型中,我添加了一个 Meta 类以获得权限: enter image description here

class task(models.Model):
    name = models.CharField(max_length=100)

    class Meta:
        permissions = (
            ('view_task', 'View task'),
        )

    def __str__(self):
        return self.name

完成 makemigrations 和 migrate 后,当我在 python manage.py shell 中运行下面的测试时,它总是调用我的用户模型中的 has_perm 函数并返回该函数的值,即 True。

>>>from myapp.models import User, task
>>>setuser = User.objects.get(email = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fa90959fba9d979b9396d4999597" rel="noreferrer noopener nofollow">[email protected]</a>')
>>>task = exclusionDomain.objects.get(name = 'task1')
>>>setuser.has_perm('view_task', task)

如何解决这个问题?或者有没有关于如何在自定义用户模型中使用监护人的有用教程?

最佳答案

如果有人遇到同样的问题。解决办法如下:

不要使用 AbstractBaseUser,而是使用 AbstractUser。 AbstractBaseUser 与 django-guardian 不兼容。根据监护人文档,使用 AbstractUser 的扩展用户模型应该没问题。它也对我有用。

这里是如何使用 AbstractUser 扩展用户模型的教程: https://wsvincent.com/django-custom-user-model-tutorial/

关于django - 如何在自定义用户模型中使用 django-guardian,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51160255/

相关文章:

python - 将所有 `None` 移动到列表的末尾 (python3)

python-3.x - Windows 10 中的 Dlib ImportError on line _dlib_pybind11 import *, DLL Load Failed

python - 我应该使用什么shebang来始终指向python3?

django-rest-framework - 使用 DjangoObjectPermissionsFilter 使用 django-guardian 过滤用户的对象

python - 如何创建动态 Django 选择字段

python - 内置登录 User.objects.all() 上的 Django 模板标签

python - django查询检索以父名称为键的子值字典?

django 管理 jQueryUI 对话框

permissions - django-guardian 和 django-rest-framework

python - 使用 Django 模型权限引发 DoesNotExist 错误