制作自定义用户表单时没有属性“set_password”

时间:2016-03-04 04:30:48

标签: python django database model

所以我正在尝试为注册和管理事务制作自定义用户表单。问题是,我尝试注册某人,我得到的错误是没有属性'set_password'。这是我的views.py:

form = RegistrationForm()
if request.method=="POST":
    if request.POST['action']=="register":
        form = RegistrationForm(request.POST or None)
        if form.is_valid():
            form.save()

这是forms.py中的RegistrationForm():

class RegistrationForm(UserCreationForm):

    fname = forms.CharField(max_length=256, label="", required=True, widget=forms.TextInput(attrs={'class': 'form-control','placeholder': 'First Name','required':True}))
    lname = forms.CharField(max_length=256, label="", required=True, widget=forms.TextInput(attrs={'class': 'form-control','placeholder': 'Last Name','required':True}))
    email = forms.EmailField(max_length=256, label="Email", required=True, widget=forms.EmailInput(attrs={'class': 'form-control','placeholder': 'Email','required':True, 'type':'email'}))
    emailConfirm = forms.EmailField(max_length=256, label="", required=True, widget=forms.EmailInput(attrs={'class': 'form-control','placeholder': 'Confirm Email','required':True, 'type':'email'}))
    password1 = forms.CharField(max_length=256, label="", required=True, widget=forms.PasswordInput(attrs={'class': 'form-control','placeholder': 'Password','required':True}))
    password2 =forms.CharField(required=False)

    biography = forms.CharField(label = "Biography",required=False)
    research_place = forms.CharField(label="Research Place",required=False)
    studies = forms.CharField(label="Studies",required=False)

    class Meta:
        model = RegisterUser
        fields = ('email','emailConfirm','password1','biography','research_place','studies')
    def save(self,commit=True):
        RegisterUser = super(RegistrationForm,self).save(commit=False)
        RegisterUser.first_name=self.cleaned_data['fname']
        RegisterUser.last_name=self.cleaned_data['lname']
        RegisterUser.biography=self.cleaned_data['biography']
        RegisterUser.research_place="Def"
        RegisterUser.studies="Def"
        print (RegisterUser.studies)

        if commit:
            print("hurr")
            RegisterUser.save()
        return RegisterUser

我的models.py:

class RegisterUser(models.Model):
    user=models.OneToOneField(User,on_delete=models.CASCADE)
    biography = models.CharField(max_length=1000000)
    research_place = models.CharField(max_length=1000000)
    studies = models.CharField(max_length=1000000)

最后是admin.py:

class RegisterInline(admin.StackedInline):
    model = RegisterUser
    can_delete = False

class UserAdmin(BaseUserAdmin):
    inlines = (RegisterInline, )

admin.site.unregister(User)
admin.site.register(User, UserAdmin)

请让我知道我做错了什么,如果还有什么我应该改变。非常感谢!

1 个答案:

答案 0 :(得分:0)

根据docs,您需要从AbstractBaseUser班级继承您的RegisterUser班级。您还需要指定一些关键实现。

引用文档

  

构建合规自定义用户模型的最简单方法是   继承自AbstractBaseUser。 AbstractBaseUser提供了核心   用户模型的实现,包括散列密码和   标记化密码重置。

您需要在自定义用户模型中指定的其他关键实现