ModelForm save()获得了意外的关键字参数'commit'

时间:2019-06-30 07:08:23

标签: python django

我尝试在django中创建自定义用户,但是遇到问题,请帮忙。

该问题是在我从管理员添加或更改用户并保存时进行的,我不知道问题出在哪里,但是我在form.py中感到,请帮助我解决此问题。

models.py


class ObUser(AbstractUser):
    SEX = (
        ('M', 'MALE'),
        ('F', 'FEMALE'),
    )
    username    = models.CharField(max_length=30, unique=True)
    email       = models.EmailField(max_length=30, unique=True, blank=False, null=False)
    first_name  = models.CharField(max_length=20, blank= False, null=False)
    last_name   = models.CharField(max_length=50, blank= False, null=False)
    password    = models.CharField(max_length=50)
    born_date   = models.DateField(auto_now=False, auto_now_add=False, blank=True, null=True)
    address     = models.TextField(blank=True, null=True)
    phone       = models.IntegerField(blank=True, null=True)
    sim_id      = models.IntegerField(blank=True, null=True)
    quotes      = models.CharField(max_length=100, blank=True, null=True)
    sex         = models.CharField(max_length=1, choices=SEX)
    is_active   = models.BooleanField(default=True)
    last_login  = models.DateTimeField(auto_now=False, auto_now_add=False, blank=True, null=True)
    last_update = models.DateTimeField(auto_now=True, auto_now_add=False, blank=True, null=True)
    date_joined = models.DateField(auto_now=False, auto_now_add=True)
    is_verified = models.BooleanField(default=False)
    objects     = ObUserManager

然后我制作ModelForm:

form.py

class ObUserCreate(forms.ModelForm):
    password1 = forms.CharField(label='password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='konfirmasi password', widget=forms.PasswordInput)


    class Meta:
        model       = ObUser
        fields      = ('username', 'email', 'first_name', 'last_name', 'password')


    def clean_password2(self):
        password1=self.cleaned_data.get('password1')
        password2=self.cleaned_data.get('password2')
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError('password tidak sama')
        return password2

    def save(self, commit=True):
        self.clean_password2()
        user = super().save(commit=False)
        user.set_password(self.cleaned_data['password2'])
        if commit:
            user.save()
        return user

class ObUserChange(forms.ModelForm):
    class Meta:
        model       = ObUser
        exclude     = ['last_login', 'last_update', 'date_joined', 'is_verified']

    def save(self):
        user = super().save()
        if first_name and last_name and born_date and address and phone and sim_id and quotes and sex:
            user.is_verified=True
            user.save()
        user.save()
        return user

和这样的管理员


class UserAdm(UserAdmin):
    form            = ObUserChange
    add_form        = ObUserCreate
    list_display    = ('username', 'email', 'is_active', 'is_verified')
    fieldsets       = (None, {'fields': ('username', 'email', 'first_name', 'last_name', 'born_date', 'address', 'phone', 'sim_id', 'sex')}),
    add_fieldsets   = (None, {'fields': ('username', 'email', 'password1', 'password2')}),  
    search_fields   = ('username',)
    ordering        = ('email',)
admin.site.register(ObUser, UserAdm)

但是我有类似的错误:

  

请求方法:POST   要求网址:http://localhost/admin/obusers/obuser/add/   Django版本:2.2.2   异常类型:TypeError   异常值:
  save()得到了意外的关键字参数'commit'   异常位置:save_form,第1082行中的D:\ project \ django \ tutorials \ env \ lib \ site-packages \ django \ contrib \ admin \ options.py   Python可执行文件:D:\ project \ django \ tutorials \ env \ Scripts \ python.exe   的Python版本:3.7.3   Python路径:
  ['D:\ project \ django \ tutorials \ otobrothers',    'C:\ Users \ masdika \ AppData \ Local \ Programs \ Python \ Python37 \ python37.zip',    'C:\ Users \ masdika \ AppData \ Local \ Programs \ Python \ Python37 \ DLLs',    'C:\ Users \ masdika \ AppData \ Local \ Programs \ Python \ Python37 \ lib',    'C:\ Users \ masdika \ AppData \ Local \ Programs \ Python \ Python37',    'D:\ project \ django \ tutorials \ env',    'D:\ project \ django \ tutorials \ env \ lib \ site-packages']   服务器时间:2019年6月30日,星期日06:26:55 +0000

先感谢

1 个答案:

答案 0 :(得分:1)

只需尝试在save()方法中添加*args, **kwargs

def save(self,*args,**kwargs):
            use  = self.clean_password2()
            user = super().save(commit=False)
            if use:
                user.set_password(self.cleaned_data['password2'])
                user.save()
            return user