簡體   English   中英

ModelForm save() 得到了一個意外的關鍵字參數“commit”

[英]ModelForm save() got an unexpected keyword argument 'commit'

我嘗試在django創建自定義用戶,但有問題,請幫忙。

問題是當我從管理員添加或更改用戶並保存它時,我不明白問題出在form.py ,但我覺得在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 請求 URL: 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 in save_form, line 1082 Python Executable: 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'] 服務器時間:Sun, 30 Jun 2019 06:26:55 +0000

之前謝謝

只需嘗試在 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM