2014-09-24 106 views
0

我觉得我错过了这个明显的东西。django admin不能与自定义用户一起工作

我创建了一个自定义用户和用户马槽

class UserManager(BaseUserManager): 
    # create a normal user 
    # an email and password must be provided 
    def create_user(self, email, password, first_name, last_name, 
        location, date_of_birth): 

     if not email: 
      raise ValueError("User must have an email") 

     if not password: 
      raise ValueError("User must have a password") 

     email = email.lower() 

     user = self.model(
       email=email, 
       first_name=first_name, 
       last_name=last_name, 
       location=location, 
       date_of_birth=date_of_birth 
       ) 

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

    # Make an administrator 
    def create_superuser(self, email, password, first_name, last_name, 
         location, date_of_birth): 
     user = self.create_user(
       email=email, 
       password=password, 
       first_name=first_name, 
       last_name=last_name, 
       location=location, 
       date_of_birth=date_of_birth 
       ) 
     user.is_admin = True 
     user.is_moderator = True 
     user.save(using=self._db) 
     return user 

class User(AbstractBaseUser): 
    email = models.EmailField(
      verbose_name='email address', 
      max_length=255, 
      unique=True 
      ) 
    first_name = models.CharField(max_length=50) 
    last_name = models.CharField(max_length=50) 
    location = models.ForeignKey(Location) 
    date_of_birth = models.DateField() 
    date_joined = models.DateTimeField(auto_now_add=True) 

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

    objects = UserManager() 

    USERNAME_FIELD = 'email' 
    REQUIRED_FIELDS = ['first_name', 'last_name', 'location', 'date_of_birth'] 

    def __unicode__(self): 
     return self.email 

    def get_full_name(self): 
     return self.first_name + ' ' + self.last_name 

    def get_age(self): 
     age = date.today() - self.date_of_birth 
     return age.days/365 

    def is_staff(self): 
     return self.is_admin 

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

    def has_module_perms(self, app_label): 
     return True 

但是如果我访问管理网站,它会很乐意授权谁是不是管理员is_admin=False

用户有没有人碰到这个问题,使用django admin与自定义用户时是否需要更改某些内容?

编辑

setting.py

AUTH_USER_MODEL = 'userAccount.User' 
AUTHENTICATION_BACKEND = (
    'django.contrib.auth.backends.ModelBackend', 
) 
+0

我假设你正确地为您定制的用户模型配置一切 – 2014-09-24 05:37:42

+0

我假设如此(在settings.py?)太?编辑以显示我在'settings.py'中为自定义用户添加的内容 – Ben 2014-09-24 06:18:07

+0

您能否详细说明一下?我调用了'UserManager'的'create_user'函数,所以设置密码应该是一样的。还有什么我需要做的吗? – Ben 2014-09-24 06:26:32

回答

1

is_admin是不是说Django的认证系统知道。在authentication form that is used,如果用户是主动还是员工它只检查:

class AdminAuthenticationForm(AuthenticationForm): 
    """ 
    A custom authentication form used in the admin app. 
    """ 
    error_messages = { 
     'invalid_login': _("Please enter the correct %(username)s and password " 
          "for a staff account. Note that both fields may be " 
          "case-sensitive."), 
    } 
    required_css_class = 'required' 

    def confirm_login_allowed(self, user): 
     if not user.is_active or not user.is_staff: 
      raise forms.ValidationError(
       self.error_messages['invalid_login'], 
       code='invalid_login', 
       params={'username': self.username_field.verbose_name} 
      ) 

在原来的用户模型,is_staff是一个模型场。你没有这样的领域,而是一种方法。这可能是为什么它不起作用。

就可以解决这个问题的方法有两种:

  1. 创建自己的AdminAuthenticationForm和调整confirm_login_allowed方法来检查is_admin而非is_staff

  2. 在您的自定义用户模型创建is_staff属性

    @property 
    def is_staff(self): 
        return self._is_admin 
    
+0

啊哈!果然我错过了@ @财产。谢谢 – Ben 2014-09-24 06:46:56

相关问题