我刚刚创建了一个自定义用户模型,但不确定应该将清理它的代码放在哪里。例如,现在电子邮件字段是唯一的,但是它不应该区分大小写,我应该把清理这个的代码放在哪里?另一个例子,我想检查以确保密码大于某个长度等等。我应该把这个代码放在哪里?任何建议表示赞赏!干杯
代码
楷模 -
class CustomUserManager(BaseUserManager):
def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
now = timezone.now()
if not email:
raise ValueError('The given email must be set')
email = self.normalize_email(email)
user = self.model(email=email,
is_staff=is_staff, is_active=True,
is_superuser=is_superuser, last_login=now,
date_joined=now, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
return self._create_user(email, password, False, False,
**extra_fields)
def create_superuser(self, email, password, **extra_fields):
return self._create_user(email, password, True, True,
**extra_fields)
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField('email address', unique=True)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
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=True,
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)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = CustomUserManager()
class Meta:
verbose_name = 'user'
verbose_name_plural = 'users'
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 email_user(self, subject, message, from_email=None):
send_mail(subject, message, from_email, [self.email])
形式 -
class CustomUserCreationForm(UserCreationForm):
def __init__(self, *args, **kargs):
super(CustomUserCreationForm, self).__init__(*args, **kargs)
class Meta:
model = User
fields = []
class CustomUserChangeForm(UserChangeForm):
def __init__(self, *args, **kargs):
super(CustomUserChangeForm, self).__init__(*args, **kargs)
class Meta:
model = User
fields = []
行政 -
class CustomUserAdmin(UserAdmin):
fieldsets = (
(None, {'fields': ('email', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
add_fieldsets = (
(None, {'fields': ('email', 'password1', 'password2')}),
('Personal info', {'fields': ('first_name', 'last_name')}),
('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser')}),
)
form = CustomUserChangeForm
add_form = CustomUserCreationForm
list_display = ('email', 'first_name', 'last_name', 'is_staff')
search_fields = ('email', 'first_name', 'last_name')
ordering = ('email',)
admin.site.register(User, CustomUserAdmin)
最佳答案
感谢您展示代码。
Form
水平和在 Model
等级。 Form
解决方案:在创建和更改表单类(我建议术语创建和更新)中添加以下方法:
def clean_email(self):
data = self.cleaned_data['email']
data = data.lower()
# Always return a value to use as the new cleaned data
return data
文档:https://docs.djangoproject.com/en/3.2/ref/forms/validation/#cleaning-a-specific-field-attributeModel
解决方案:将以下方法添加到您的自定义 User 类:
def clean(self):
# This method can be used to raise non field specific ValidationErrors or cleaning the data.
if (self.email){
self.email = self.email.lower()
}
def save(self, *args, **kwargs):
# This overriding of the save method is necessary because Django by default does not call the
# full_clean() method and there is where the clean() method is called
self.full_clean()
return super().save(*args, **kwargs)
文档:https://docs.djangoproject.com/en/3.2/ref/models/instances/#django.db.models.Model.cleanAUTH_PASSWORD_VALIDATORS
.更具体地说,您可以在项目 django 设置文件中编辑它们,settings.py
默认情况下,它们具有以下内容:AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
第二个验证器称为 MinimumLengthValidator
默认情况下,最小长度为 8 个字符。但是,您可以通过添加这样的选项字典来覆盖它:{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'OPTIONS': {
'min_length': 9,
}
}
文档:https://docs.djangoproject.com/en/3.2/topics/auth/passwords/#enabling-password-validation
关于django - 清理自定义用户模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35477848/