【问题标题】:Custom user model in admin管理员中的自定义用户模型
【发布时间】:2013-04-11 07:38:11
【问题描述】:

我尝试创建一个自定义用户模型来向用户添加一些自定义字段。我使用了 django 1.5 中引入的基于 AbstractBaseUser 的新方法。一切(登录)工作,除了管理面板。登录管理界面时,我收到以下错误:

AttributeError at /admin/
'ShopUser' object has no attribute 'is_superuser'

这是我的模型:

from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
from django.contrib import auth

class ShopUserManager(BaseUserManager):

  def create_user(self, email, password=None):
    if not email:
      raise ValueError("We need an e-mail here...")

    user = self.model(
      email = ShopUserManager.normalize_email(email),
    )

    user.set_password(password)
    user.save(using=self._db)
    return user

  def create_superuser(self, email, password):
    user = self.create_user(
      email,
      password = password,
    )
    user.is_admin = True
    user.is_staff = True
    user.save(using=self._db)
    return user


class ShopUser(AbstractBaseUser):
  email = models.EmailField(
    verbose_name = 'e-mail address',
    max_length = 255,
    unique = True,
    db_index = True,
  )

  is_active = models.BooleanField(default=True)
  is_admin = models.BooleanField(default=False)
  is_staff = models.BooleanField(default=False)

  objects = ShopUserManager()

  USERNAME_FIELD = 'email'
  # REQUIRED_FIELDS = ['']

  def __unicode__(self):
    return self.email

  def has_perms(self, perm_list, obj=None):
    """
    Returns True if the user has each of the specified permissions. If
    object is passed, it checks if the user has all required perms for this
    object.
    """
    for perm in perm_list:
        if not self.has_perm(perm, obj):
            return False
    return True

  def has_module_perms(self, app_label):
    """
    Returns True if the user has any permissions in the given app label.
    Uses pretty much the same logic as has_perm, above.
    """
    # Active superusers have all permissions.
    if self.is_active and self.is_superuser:
        return True

    return _user_has_module_perms(self, app_label)

对此有何建议?谢谢!

【问题讨论】:

  • 您的帐户是is_superuser=True 吗?您可以直接在您的数据库中查看它
  • 我的数据库中没有超级用户字段
  • hmmmm....您的代码中缺少某些内容
  • 好的,我将超级用户属性添加到模型中,它现在可以工作了-.-
  • 好,我想我不必回答。在您的create_superuser 中还有一件事,您忘记输入user.is_superuser = True

标签: django


【解决方案1】:

您不必让您的类从 PermissionsMixin 继承。

我遇到了同样的问题,我通过向我的用户类(在您的情况下为 ShopUser)添加一些必需的方法来解决它。

class ShopUser(AbstractBaseUser):

  ...


  def get_full_name(self):
    return self.fullname

  def get_short_name(self):
    return self.shortname

  @property
  def is_superuser(self):
    return self.is_admin

  @property
  def is_staff(self):
    return self.is_admin

  def has_perm(self, perm, obj=None):
    return self.is_admin

  def has_module_perms(self, app_label):
    return self.is_admin

【讨论】:

  • 同意。这是最简单的解决方案。第一次工作
  • 为什么这不是公认的答案?这是完美的解决方案。
【解决方案2】:

ShopUser 类继承自PermissionsMixin,如下所示:

class ShopUser(AbstractBaseUser, PermissionsMixin):

这将添加 is_superuser 字段,并且应该与管理 UI 很好地配合。

你的create_superuser方法应该设置user.is_superuser = True

最后,您应该为您的ShopUser 类实现get_full_nameget_short_name

更多信息在这里:Customizing authentication in Django

【讨论】:

    猜你喜欢
    • 2013-06-01
    • 2012-08-24
    • 2019-10-05
    • 2013-06-26
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 2014-07-11
    相关资源
    最近更新 更多