2016-03-02 98 views
0

我正在构建一个使用电子邮件作为用户名的自定义用户。 当我运行的服务器上创建一个自定义的管理员,我得到这个错误为什么Django抛出系统错误?

class 'user.admin.UserAdmin'>: (admin.E116) The value of 
'list_filter[0]' refers to 'is_staff', which does not refer to a 
Field. 

这里是我的admin.py

from django import forms 
    from django.contrib import admin 
    from django.contrib.auth.models import Group 
    from django.contrib.auth.admin import UserAdmin 
    from django.contrib.auth.forms import ReadOnlyPasswordHashField 

    from .models import BaseUser 

class UserCreationForm(forms.ModelForm): 
    """ 
    A form for creating new users. Includes all the required 
    fields, plus a repeated password. 
    """ 
    password1 = forms.CharField(
     label='Password', 
     widget=forms.PasswordInput 
    ) 
    password2 = forms.CharField(
     label='Password confirmation', 
     widget=forms.PasswordInput 
    ) 

    class Meta: 
     model = BaseUser 
     fields = ('email',) 

    def clean_password2(self): 
     #Check that the two password entries match 
     password1 = self.cleaned_data.get('password1') 
     password2 = self.cleaned_data.get('password2') 

     if password1 and password2 != password2: 
      raise forms.ValidationError('Password do not 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): 
     """ 
     A form for updating users. Includes all the fields on 
     the user, but replaces the password field with admin's 
     password hash display field. 
     """ 

     password = ReadOnlyPasswordHashField() 

     class Meta: 
      model = BaseUser 
      fields = (
       'email', 
       'password', 
       'user_first_name', 
       'user_last_name', 
       'user_mobile', 
       'is_a_student', 
      ) 

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

class UserAdmin(UserAdmin): 
#Forms to add and change user instances 
     form = UserChangeForm 
     add_form = UserCreationForm 

     #The fields to be used in displaying User model. 
     #These overried the definitions on the base UserAdmin 
     #That reference specific fields on auth.User 


     list_display = (
      'email', 
     ) 
     list_filter = ('is_staff',) 

     fieldsets = (
      (None, {'fields': ('email', 'password')}), 
      ('Personal info', {'fields': (
       'user_first_name', 
       'user_last_name', 
       'user_mobile', 
      )}), 
      ('Permission', {'fields': (
       'is_a_student', 
      )}) 
     ) 

     # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin 
     # overrides get_fieldsets to use this attribute when creating a user. 
     add_fieldsets = (
      (None, { 
       'classes': ('wide',), 
       'fields': (
        'email', 
        'password1', 
        'password2', 
        'user_first_name', 
        'user_last_name', 
        'user_mobile', 
        'is_a_student', 
       )} 
      ), 
     ) 


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

#Register the new UserAdmin   
admin.site.register(BaseUser, UserAdmin) 
admin.site.unregister(Group) 


下面的代码是models.py

from django.db import models 
from django.core.validators import RegexValidator 
from django.core.urlresolvers import reverse 
from django.utils import timezone 
from django.core.mail import send_mail 
from django.utils.http import urlquote 
from django.utils.translation import ugettext_lazy as _ 
from django.core.mail import send_mail 
from django.core.files.storage import FileSystemStorage 
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser, PermissionsMixin 
) 


class MyUserManager(BaseUserManager): 
    def create_user(self, email, password=None): 
     """ 
     Creates and saves a User with the given email and password 
     """ 
     if not email: 
      raise ValueError('Users must have an email address') 

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

    def create_superuser(self, email, password): 
     """ 
     Creates and saves a superuser with the given email and password 
     """ 
     user = self.create_user(
      email=email, 
      password=password, 
     ) 
     user.is_superuser = user.is_staff = True 
     user.save(using=self._db) 
     return user 


class BaseUser(AbstractBaseUser, PermissionsMixin): 
    email = models.EmailField(
     verbose_name='email', 
     max_length=255, 
     unique=True, 
    ) 

    user_first_name = models.CharField(max_length=30) 
    user_last_name = models.CharField(max_length=50) 
    mobile_regex = RegexValidator(regex=r'^\+?1\d{9,15}$', message="Please enter a max of 10 digits :)") 
    user_mobile = models.CharField(validators=[mobile_regex], blank=True, max_length=10) 
    is_a_student = models.BooleanField(default=False) 
    is_staff = models.BooleanField(default=False) 
    is_active = models.BooleanField(default=True) 
    date_joined = models.DateTimeField(auto_now_add=True) 

    objects = MyUserManager() 

    USERNAME_FIELD = 'email' 
    REQUIRED_FIELDS = [] 

    class Meta: 
     verbose_name = 'User' 
     verbose_name_plural = 'Users' 

    def get_full_name(self): 
     return self.email 

    def get_short_name(self): 
     return self.email 

    def __str__(self): 
     return self.email 

    def has_perm(self, perm, obj=None): 
     """ 
     Does the user have a specific permission? 
     """ 
     return True 

    def is_student(self): 
     return self.is_a_student 

    @property 
    def is_staff(self): 
     """ 
     Is the user a member of staff? 
     """ 
     return self.is_staff 

    def emai_user(self, subject, message, from_email=None): 
     send_mail(subject, message, from_email, [self.email]) 

选项已完成
- 我已经拿掉了admin.py中的所有is_staff属性,但仍然出现错误。
- 重构了很多次,以检查问题是否出现在我的代码的不同区域。

在这一点上,我卡住了。有人可以帮助调试吗?

回答

0

由于Django关于现场的“会谈”,我会在第一次审查中检查模型 我找不到模型中的任何密码或密码1和密码2字段。

+0

工作的测试我们的模型的感谢! – chuck

0
@property 
    def is_staff(self): 
     """ 
     Is the user a member of staff? 
     """ 
     return self.is_staff 

上面的代码创建了一个导致错误的递归执行。

@property 
    def is_admin(self): 
     """ 
     Is the user a member of staff? 
     """ 
     return self.is_staff 
0
#list_filter = ('is_staff',) 
list_filter = ('is_admin',) 
相关问题