【问题标题】:ModelForm save() got an unexpected keyword argument 'commit'ModelForm save() 得到了一个意外的关键字参数“提交”
【发布时间】:2019-06-30 07:08:23
【问题描述】:

我尝试在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' 异常位置:D:\project\django\tutorials\env\lib\site-packages\django\contrib\admin\options.py 在 save_form,第 1082 行 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

感谢之前

【问题讨论】:

    标签: python django


    【解决方案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
    

    【讨论】:

    • 它仍然出错 save() got an unexpected keyword argument 'commit'
    • @blanksix 更新了我的答案。尝试使用其他名称而不是 save 来保存密码
    • 如果我们将方法更改为 save_password 是不是在表单尝试保存时不起作用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 2013-06-07
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    相关资源
    最近更新 更多