gpt4 book ai didi

django - 类型错误 : save() got an unexpected keyword argument 'force_insert'

转载 作者:行者123 更新时间:2023-12-04 04:24:22 25 4
gpt4 key购买 nike

当我尝试注册新用户时出现此错误:

...
File "/home/django/django_project/accounts/views.py" in form_valid
34. new_user = User.objects.create(username=username, email=email)

File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in manager_method
85. return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in create
394. obj.save(force_insert=True, using=self.db)

File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/base_user.py" in save
80. super(AbstractBaseUser, self).save(*args, **kwargs)

File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save
808. force_update=force_update, update_fields=update_fields)

File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
848. update_fields=update_fields, raw=raw, using=using,

File "/usr/local/lib/python2.7/dist-packages/django/dispatch/dispatcher.py" in send
193. for receiver in self._live_receivers(sender)

File "/home/django/django_project/accounts/models.py" in post_save_user_receiver
150. new_profile = UserProfile.objects.get_or_create(user=instance)

File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in manager_method
85. return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in get_or_create
466. return self._create_object_from_params(lookup, params)

File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in _create_object_from_params
500. obj = self.create(**params)

File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in create
394. obj.save(force_insert=True, using=self.db)

Exception Type: TypeError at /register/
Exception Value: save() got an unexpected keyword argument 'force_insert'

以下是参与此错误的模型和 View :
帐户/模型.py
class UserProfile(models.Model):
....

def save(self):
super(UserProfile,self).save()
img = Image.open(self.image.path)

if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.image.path)



def post_save_user_receiver(sender, instance, created, *args, **kwargs):
if created:
new_profile = UserProfile.objects.get_or_create(user=instance)

if UserProfile.stripe_customer_id is None or UserProfile.stripe_customer_id == '':
new_customer_id = stripe.Customer.create(email=instance.email)
free_membership = Membership.objects.get(membership_type='Free')
UserProfile.stripe_customer_id = new_customer_id['id']
UserProfile.membership = free_membership
UserProfile.save()


post_save.connect(post_save_user_receiver, sender=settings.AUTH_USER_MODEL)
帐户/ View .py
class UserRegisterView(FormView):
template_name = 'accounts/user_register_form.html'
form_class = UserRegisterForm
success_url = '/login'

def form_valid(self, form):
username = form.cleaned_data.get("username")
email = form.cleaned_data.get("email")
password = form.cleaned_data.get("password")
new_user = User.objects.create(username=username, email=email)
new_user.set_password(password)
new_user.save()
messages.info(self.request, 'Welcome')
return super(UserRegisterView, self).form_valid(form)


def profile_view(request):
if request.method == 'POST':
p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
if p_form.is_valid():
p_form.save()
messages.info(request, 'Saved')
return redirect('/')
else:
p_form = ProfileUpdateForm() #instance=request.user.profile

context = {
'p_form' : p_form,
}
return render(request,'accounts/userprofile.html',context)
发生了什么的任何暗示?

最佳答案

save(..) method [Django-doc]Model需要一些额外的可选参数。签名确实是:

Model.save(force_insert=False, force_update=False, using=DEFAULT_DB_ALIAS, update_fields=None)

If you want customized saving behavior, you can override this save() method. See Overriding predefined model methods for more details.



因此,如果您覆盖 save(..)功能,最好尊重签名。您不必自己指定这些参数,但可以使用 *args**kwargs将位置和命名参数传递给底层 save方法:
class UserProfile(models.Model):

# ...

def save(self, *args, **kwargs):
super(UserProfile,self).save(*args, **kwargs)
if self.image:
img = Image.open(self.image.path)
if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.image.path)

关于django - 类型错误 : save() got an unexpected keyword argument 'force_insert' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58157297/

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