2013-12-19 232 views
0

我通過擴展AbstractBaseUser類來創建自定義用戶。我已經在線閱讀了Django文檔和幾個教程,我認爲到目前爲止我所做的工作非常基礎,應該可以工作。Django自定義用戶管理登錄

我面臨的問題是,即使我從頭開始創建一個新的數據庫並執行syncdb,manage.py控制檯不會要求我輸入管理員用戶名和密碼(正如通常那樣)。因此,我無法訪問/管理員。

問題是部分(錯誤地)解決了我使用AUTH_PROFILE_MODULE而不是AUTH_USER_MODEL。然而這導致'用戶'和'MyUser'在管理頁面上顯示爲不同的對象,即MyUser未被設置爲默認用戶。 (顯然)

models.py

class UserManager(BaseUserManager): 
    def create_user(self, username, email, corp, password=None): 
     if not (username or email or corp): 
      raise ValueError('Users must have an username, email and corporation') 
     user = self.model(username=username, email=UserManager.normalize_email(email), corp=corp,) 
     user.set_password(password) 
     user.is_active = True 
     user.save(using=self._db) 
     return user 

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


class MyUser(AbstractBaseUser, PermissionsMixin): 
    username = models.CharField(max_length=254, unique=True, blank=False) 
    first_name = models.CharField(max_length=30, blank=True) 
    last_name = models.CharField(max_length=30, blank=True) 
    email = models.EmailField(blank=True) 
    corp = models.ForeignKey(Client, related_name="is_employee_of") 

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

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

    def get_short_name(self): 
     return self.first_name 

    def __unicode__(self): 
     return self.username + "@" + self.corp.shortName 

    objects = UserManager() 

    USERNAME_FIELD = 'username' 
    REQUIRED_FIELDS = ['email', 'corp'] 

admin.py

admin.site.register(Client) 
admin.site.register(Address) 

class UserCreationForm(forms.ModelForm): 
    password1 = forms.CharField(label='Password', widget=forms.PasswordInput) 
    password2 = forms.CharField(label='Password Confirmation', widget=forms.PasswordInput) 

    class Meta: 
     model = MyUser 
     fields = ('username', 'email', 'corp', 'first_name', 'last_name') 

    def clean_password2(self): 
     password1 = self.cleaned_data.get("password1") 
     password2 = self.cleaned_data.get("password2") 

     if password1 and password2 and password1 != password2: 
      raise forms.ValidationError("Passwords don't match") 

     return password2 

    def save(self, commit=True): 
     user = super(UserCreationForm, self).save(commit=False) 
     user.set_password(self.cleaned_data["password1"]) 

     if commit: 
      user.save() 
     return user 

class UserChangeForm(forms.ModelForm): 
    password = ReadOnlyPasswordHashField() 

    class Meta: 
     model = MyUser 

    def clean_password(self): 
     return self.initial["password"] 

class MyUserAdmin(UserAdmin): 
    form = UserChangeForm 
    add_form = UserCreationForm 

    list_display = ('username', 'email', 'corp', 'first_name', 'last_name', 'is_admin', 'is_staff') 
    list_filter = ('is_admin',) 
    fieldsets = (
     (None, {'fields': ('username', 'email', 'password')}), 
     ('Personal Info', {'fields': ('first_name', 'last_name', 'corp')}), 
     ('Permissions', {'fields': ('is_admin', 'is_staff')}), 
     ('Important Dates', {'fields': ('last_login',)}), 
    ) 

    add_fieldsets = (
     (None, { 
      'classes': ('wide',), 
      'fields': ('username', 'email', 'password1', 'password2')} 
     ), 
    ) 

    search_fields = ('username',) 
    ordering = ('username',) 
    filter_horizontal =() 

admin.site.register(MyUser, MyUserAdmin) 

settings.py

INSTALLED_APPS = (
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'django.contrib.admin', 
    'Profiles', 
) 
AUTHENTICATION_BACKENDS = (
     'django.contrib.auth.backends.ModelBackend', 
) 
AUTH_USER_MODEL = "Profiles.MyUser" 

回答

0

我不知道這是否有幫助,但也許你可以使用燈具加載初始數據: https://docs.djangoproject.com/en/dev/howto/initial-data/

然後,當你的模型創建時,它會自動填充第一個管理員的值。

+0

這看起來像一個聰明的黑客。我會嘗試,但它仍然不會告訴我爲什麼我首先面臨這個問題。同樣的錯誤也可能在未來導致更多的問題。 – vinayakshukl