使用自定义模型身份验证登录管理员

时间:2014-08-27 23:49:10

标签: python django django-admin django-authentication

所以我在django中使用名为accounts的模型进行身份验证,而不是默认的User。一切都还可以,但是当我尝试登录django管理员它不会让我,它说不正确的用户,尽管已经从控制台创建了一个用户“manage.py createsuperuser”

有什么想法吗? 我的代码

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

    class Meta:
        model = accounts
        fields = ('username', '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 MyUserAdmin(UserAdmin):
    form = UserChangeForm
    add_form = UserCreationForm

    list_display = ('email', 'birthday', 'is_admin')
    list_filter = ('is_admin',)
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        ('Personal info', {'fields': ('birthday',)}),
        ('Permissions', {'fields': ('is_admin',)}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'birthday', 'password1', 'password2')}
        ),
    )
    search_fields = ('email',)
    ordering = ('email',)
    filter_horizontal = ()

    admin.site.register(accounts, MyUserAdmin)
    admin.site.unregister(Group)

models.py

class AccountsManager(BaseUserManager):
    def create_superuser(self, email, username, password):
        user = self.create_user(email=email,
            password=password,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user

class accounts(AbstractBaseUser):
    username = models.CharField(max_length=50, null=True, unique=True)
    name = models.CharField(max_length=50, null=True)
    lastname = models.CharField(max_length=50, null=True)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    def get_full_name(self):
        return self.email

    def get_short_name(self):
        return self.email

    def __str__(self):              # __unicode__ on Python 2
        return self.email

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

    def has_module_perms(self, app_label):
        return True

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

    objects = AccountsManager()    

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

1 个答案:

答案 0 :(得分:0)

解决了它,错误是我没有注册用户 改变

user = self.create_user(email=email,
        password=password,
    )

user = self.create_user(email=email,
        password=password,
        username=username
    )
相关问题