我有一个自定义的用户模型。我为此编写了一个自定义ModelAdmin。以下是ModelAdmin:
class UserAdmin(BaseUserAdmin):
form = UserAdminChangeForm
# add_form = UserAdminCreationForm
list_display = ['complete_name', 'phone_number', 'admin', 'staff']
list_filter = ['admin', 'staff', 'is_active']
fieldsets = (
(None, {'fields': ('phone_number', 'password')}),
('Personals', {'fields': ('complete_name', 'email',
'expertise', 'image_profile')}),
('Permissions', {'fields': ('admin', 'staff', 'is_active',)}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': (
'phone_number',
'complete_name',
'admin',
'staff',
'is_active',
'password',
'password2'
),
}),
)
问题是,当我尝试从管理面板创建用户时。面板显示此错误:
请更正以下错误。
,第一个字段突出显示。
,但不显示我创建的任何错误消息。 如何查找验证错误?
已编辑 以下是用户模型和管理员。
class UserManger(BaseUserManager):
def create_user(self, phone_number, email=None, password=None, admin=False, staff=False):
if not phone_number:
raise ValueError('You need to enter your phone number without zero at beginning.')
if not check_phone_number(phone_number):
raise ValueError('You should enter your phone number correctly.')
if not password:
raise ValueError('You need to enter a password')
user_obj = self.model(
phone_number=phone_number,
email=self.normalize_email(email)
)
user_obj.set_password(password)
user_obj.is_active = True
user_obj.admin = admin
user_obj.staff = staff
user_obj.save(using=self._db)
return user_obj
def create_contractoruser(self, phone_number, email=None, full_name=None, password=None):
user = self.create_user(
phone_number=phone_number, email=email, password=password,
staff=True
)
return user
def create_superuser(self, phone_number, email=None, password=None):
user = self.create_user(
phone_number=phone_number, email=email, password=password,
admin=True, staff=True
)
return user
class User(AbstractBaseUser):
phone_number = models.CharField('شماره تلفن', unique=True, max_length=11)
email = models.EmailField('آدرس ایمیل', max_length=255, unique=True, blank=True, null=True)
complete_name = models.CharField('نام کامل', max_length=255, null=True, blank=True)
expertise = models.ForeignKey(ServiceCategory, blank=True, null=True, verbose_name='تخصص')
image_profile = models.ImageField('عکس کاربر', blank=True, null=True, upload_to='profile-images')
timestamp = jModels.jDateTimeField('تاریخ عضویت', auto_now_add=True)
is_active = models.BooleanField('فعال بودن کاربر', default=True)
staff = models.BooleanField('پیمانکار بودن کاربر', default=False)
admin = models.BooleanField('ادمین بودن کاربر', default=False)
USERNAME_FIELD = 'phone_number'
REQUIRED_FIELDS = ['email', ]
objects = UserManger()