2014-09-24 105 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

相關問題