自定义和非管理用户模型/滞后用户表



我正在尝试使django-guardian与我的自定义用户模型一起工作。该模型继承自AbstractUserGuardianUserMixin。这是User型号:

class User(AbstractUser, GuardianUserMixin):
    class Meta:
        managed = False
        db_table = 'users'

我面临的问题是在检查权限时。例如这里的request.user.has_perm(...)

这是错误消息:

ProgrammingError at /manageDb/mira/follow/14/ relation "users_groups" does not exist LINE 1: ...sion"."group_id" = "auth_group"."id" ) INNER JOIN "users_gro...

追溯:

    File "/home/khajvah/Project/mira_website/manageDb/views.py" in delete
  486.             if request.user.has_perm('follow', follow_object):
File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/models.py" in has_perm
  353.         return _user_has_perm(self, perm, obj)
File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/models.py" in _user_has_perm
  281.             if backend.has_perm(user, perm, obj):
File "/usr/local/lib/python3.4/dist-packages/guardian/backends.py" in has_perm
  89.         return check.has_perm(perm, obj)
File "/usr/local/lib/python3.4/dist-packages/guardian/core.py" in has_perm
  54.         return perm in self.get_perms(obj)
File "/usr/local/lib/python3.4/dist-packages/guardian/core.py" in get_perms
  110.                 perms = list(set(chain(user_perms, group_perms)))
File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py" in __iter__
  162.         self._fetch_all()
File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py" in _fetch_all
  965.             self._result_cache = list(self.iterator())
File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py" in iterator
  1217.             for row in compiler.results_iter():
File "/usr/local/lib/python3.4/dist-packages/django/db/models/sql/compiler.py" in results_iter
  794.             results = self.execute_sql(MULTI)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/sql/compiler.py" in execute_sql
  840.             cursor.execute(sql, params)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py" in __exit__
  97.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python3.4/dist-packages/django/utils/six.py" in reraise
  658.             raise value.with_traceback(tb)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

问题似乎是guardian正在查询一个不存在的表users_groups,而它被称为auth_groups,它是由django.auth创建的。因此,问题归结为在guardian 中指定用户组模型

我做了一个破解的解决方案。guardian所做的是期望ManyToManyUserauth_group的关系。AbstractUser默认情况下会这样做,但由于我手动给定了managed = Falsedb_table,Django在迁移时没有创建关系,所以我为关系创建了一个中间模型,下面是我的model.py的一部分:

class User(AbstractBaseUser, PermissionsMixin, GuardianUserMixin):
    username=models.CharField(unique=True, max_length=123)
    date_joined = models.DateTimeField(default=datetime.now())
    email=models.CharField(unique=True, max_length=125)
    first_name=models.CharField(max_length=128)
    last_name=models.CharField(max_length=128)
    is_staff = models.NullBooleanField(null=True)
    is_active = models.NullBooleanField(null=True)
    USERNAME_FIELD='username'
    REQUIRED_FIELDS = ['password', 'email', 'first_name', 'last_name']
    objects = UserManager()
    class Meta:
        managed = False
        db_table = 'users'
class GroupUser(models.Model):
    group = models.ForeignKey(Group)
    user = models.ForeignKey(User)
    class Meta:
        db_table = 'users_groups'

关键是要使组表具有正确的名称。即监护人正在等待userTableName_groups

相关内容

  • 没有找到相关文章

最新更新