我创建了自定义用户
class CoinUser(AbstractBaseUser):
username = models.CharField(_('username'), max_length=30, unique=True,
help_text=_('Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters'),
validators=[
validators.RegexValidator(re.compile('^[\w.@+-]+$'), _('Enter a valid username.'), _('invalid'))
])
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
first_name = models.CharField(_('first name'), max_length=30, blank=True, null=True)
patronymic = models.CharField(_('patronymic'), max_length=30,
help_text=_('Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters'))
last_name = models.CharField(_('last name'), max_length=30, blank=True, null=True)
email = models.EmailField(_('email address'), max_length=255, unique=True,
validators=[
validators.RegexValidator(re.compile('^.+@.+\..+$', flags=re.IGNORECASE), _('Enter a valid email.'), _('invalid'))
])
post_code = models.CharField(_('post code'), max_length=10)
is_staff = models.BooleanField(_('staff status'), default=False,
help_text=_('Designates whether the user can log into this admin site.'))
is_active = models.BooleanField(_('active'), default=False,
help_text=_('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'))
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
phone = models.CharField(_('phone number'), max_length=12, blank=True, null=True, unique=True,
validators=[
validators.RegexValidator(re.compile('^[0-9]{12,12}$', flags=re.IGNORECASE), _('Enter a valid phone number.'), _('invalid'))
])
objects = CoinUserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email', 'phone']
def get_full_name(self):
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
return self.first_name
def __str__(self):
return self.username
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
class Meta:
managed = True
db_table = "co_user"
verbose_name = _("Coin user")
verbose_name_plural = _("Coin users")
然后我创建了 UserAdmin
class CoinUserAdmin(UserAdmin):
form = UserChangeFrom
add_form = UserCreationForm
list_display = ('email', 'username', 'phone')
list_filter = ('is_admin',)
fieldsets = (
(None, {'fields': ('username',)}),
('Contact info', {'fields':('email','phone')}),
)
search_fields = ('username', 'email', 'phone')
ordering = ('username', 'email',)
filter_horizontal = ()
并添加了 UserCreationForm
class UserCreationForm(forms.ModelForm):
"""docstring for UserCreationForm"""
password1 = forms.CharField(label=_('Password'), widget=forms.PasswordInput)
password2 = forms.CharField(label=_('Password confirmation'), widget=forms.PasswordInput)
class Meta:
model = CoinUser
fields = ('email', 'password', 'username', 'phone')
def clean_password2(self):
#Check that two password entires match
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):
#Save the provided password in hashed format
user = super(UserCreationForm, self).save(commit=False)
# user.phone = self.cleaned_data["phone"]
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
它工作正常,但是当我尝试添加电子邮件和电话时
email = forms.CharField(label=_("Email"), widget=forms.EmailField)
phone = forms.CharField(label=_("Phone number"), widget=forms.TextInput)
字段到 UserCreationForm 他们没有查看,当我尝试在没有这些字段的情况下保存我的用户时,我收到错误“'EmailField'对象没有属性'value_from_datadict'”