NOT NULL 约束失败:accounts_myuser.password
NOT NULL constraint failed: accounts_myuser.password
我正在 django 中做一个网络应用程序,用户可以在其中创建帐户。我将用户的密码以明文形式存储,因为我系统中的身份验证并不完全取决于密码,还取决于 otp。我在 POST 注册请求时突然遇到的问题(之前工作正常)是 "NOT NULL constraint failed: accounts_myuser.password"。我尝试删除数据库和迁移并重新迁移,但没有帮助。我给 ModelForm 和模型(自定义一个)below.I 只有两个字段,即我的模型中描述的 'email' 和 'username'。它运行良好,我可以使用下面的代码更早地成功注册用户。谁能帮帮我?
forms.py
class UserCreationForm(forms.ModelForm):
password1 = forms.IntegerField(label='Password', min_value=0000, max_value=9999, widget=forms.PasswordInput)
password2 = forms.IntegerField(label='Password Confirmation', min_value=0000, max_value=9999,
widget=forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'email']
def clean_password1(self):
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords do not match!")
if len(str(password1)) != 4 or len(str(password2)) != 4:
raise forms.ValidationError("Passwords should be of length Four!")
return password2
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.password = self.cleaned_data['password1']
if commit:
user.save()
return user
models.py
class MyUserManager(BaseUserManager):
def create_user(self, username, email, password=None):
if not email:
raise ValueError('Users must have an email')
user = self.model(
username = username,
email = self.normalize_email(email),
password = password
)
# user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, username, email, password=None):
user = self.create_user(
username, email, password
)
user.is_admin = True
user.is_staff = True
user.set_password(password)
user.save(using=self._db)
return user
TLDR;
将 password
字段添加到 Meta class 的 fields
as
class Meta:
model = User
fields = ['username', 'email', <b>'password'</b>]
如果您不将字段添加到 fields
,Django 将不会 take/accept 来自 form
[=15 的数据=]
当您调用方法 create_user
或 create_superuser
时,您是否为 password
关键字传递了有效值?
因为,如果您将空密码 (None
) 传递给这两种方法中的任何一种,这肯定会抛出
"NOT NULL constraint failed" 密码字段错误。
删除 save() 方法
刷新页面
添加数据(不会在数据库中添加)
提交数据
再次添加 save() 方法
我正在 django 中做一个网络应用程序,用户可以在其中创建帐户。我将用户的密码以明文形式存储,因为我系统中的身份验证并不完全取决于密码,还取决于 otp。我在 POST 注册请求时突然遇到的问题(之前工作正常)是 "NOT NULL constraint failed: accounts_myuser.password"。我尝试删除数据库和迁移并重新迁移,但没有帮助。我给 ModelForm 和模型(自定义一个)below.I 只有两个字段,即我的模型中描述的 'email' 和 'username'。它运行良好,我可以使用下面的代码更早地成功注册用户。谁能帮帮我?
forms.py
class UserCreationForm(forms.ModelForm):
password1 = forms.IntegerField(label='Password', min_value=0000, max_value=9999, widget=forms.PasswordInput)
password2 = forms.IntegerField(label='Password Confirmation', min_value=0000, max_value=9999,
widget=forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'email']
def clean_password1(self):
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords do not match!")
if len(str(password1)) != 4 or len(str(password2)) != 4:
raise forms.ValidationError("Passwords should be of length Four!")
return password2
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.password = self.cleaned_data['password1']
if commit:
user.save()
return user
models.py
class MyUserManager(BaseUserManager):
def create_user(self, username, email, password=None):
if not email:
raise ValueError('Users must have an email')
user = self.model(
username = username,
email = self.normalize_email(email),
password = password
)
# user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, username, email, password=None):
user = self.create_user(
username, email, password
)
user.is_admin = True
user.is_staff = True
user.set_password(password)
user.save(using=self._db)
return user
TLDR;
将 password
字段添加到 Meta class 的 fields
as
class Meta:
model = User
fields = ['username', 'email', <b>'password'</b>]
如果您不将字段添加到 fields
,Django 将不会 take/accept 来自 form
[=15 的数据=]
当您调用方法 create_user
或 create_superuser
时,您是否为 password
关键字传递了有效值?
因为,如果您将空密码 (None
) 传递给这两种方法中的任何一种,这肯定会抛出
"NOT NULL constraint failed" 密码字段错误。
删除 save() 方法
刷新页面
添加数据(不会在数据库中添加)
提交数据
再次添加 save() 方法