gpt4 book ai didi

python - ModelForm save() 得到了一个意外的关键字参数 'commit'

转载 作者:太空宇宙 更新时间:2023-11-04 04:08:22 25 4
gpt4 key购买 nike

我尝试在 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)

但我有这样的错误:

Request Method: POST Request URL: http://localhost/admin/obusers/obuser/add/ Django Version: 2.2.2 Exception Type: TypeError Exception Value:
save() got an unexpected keyword argument 'commit' Exception Location: 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 Version: 3.7.3 Python Path:
['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'] Server time: 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

关于python - ModelForm save() 得到了一个意外的关键字参数 'commit',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56823053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com