NOT NULL约束失败:accounts_myuser.password [英] NOT NULL constraint failed: accounts_myuser.password

查看:276
本文介绍了NOT NULL约束失败:accounts_myuser.password的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在django中创建一个Web应用程序,用户可以在其中创建帐户.我将用户密码以明文形式存储,因为在我的系统中,身份验证并不完全取决于密码,还取决于otp.在POST的注册请求中,我突然遇到的问题(该方法以前工作得很好)是"NOT NULL约束失败:accounts_myuser.password".我尝试删除数据库和迁移,然后重新迁移,但没有帮助.我在下面提供了ModelForm和Model(自定义),我只有两个字段,即模型中描述的``电子邮件''和``用户名''.它工作正常,我可以使用以下代码更早地成功注册用户.谁能帮我吗?

I am doing a web application in django where users can create accounts. I'm storing the users' passwords in plaintext as authentication in my system does not totally depend on password but also on the otp. The problem I'm facing all of a sudden(it worked fine earlier) at the POST request of registration, is "NOT NULL constraint failed: accounts_myuser.password". I tried deleting database and migrations and re-migrated but it didn't help. I'm giving the ModelForm and the Model(custom one) below.I've only two fields namely 'email' and 'username' described in my model. It worked fine and I could register users successfully earlier with the code below. Could anyone please help me?

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

推荐答案

调用方法create_usercreate_superuser时,是否要传递password关键字的有效值?

When you call the methods create_user or create_superuser, are you passing valid values for the password keyword?

因为,如果您将空密码(None)传递给这两种方法中的任何一种,则肯定会抛出一个 passwerod字段上出现"NOT NULL约束失败"错误.

Because, if you pass a null password (None) to any of the two method, this would definitely throw a "NOT NULL constraint failed" error on the passwerod field.

这篇关于NOT NULL约束失败:accounts_myuser.password的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆