我在Django 2.2中创建了一个博客应用。现在我有一个注册用户问题。结果在/ reg /处给出TypeError save()有一个意外的关键字参数'force_insert'。这些是我的views.py和models.py代码:
def register(request):
if request.method == "POST":
form = UserOurRegistration(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
messages.success(request, f"Akkaunt {username} muvaffaqiyatli ravishda yaratildi, akkauntga kirish uchun login va parolingizni kiriting")
return redirect('user')```
And this is code of models.py:
`from django.db import models
from django.contrib.auth.models import User
from PIL import Image
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
img = models.ImageField(default='default.jpg', upload_to='user_images')
def __str__(self):
return f'Foydalanuvchi {self.user.username} profili'
def save(self, *args, **kwargs):
super(Profile, self).save(*args, **kwargs)
image = Image.open(self.img.path)
if image.height > 64 or image.width > 64:
resize = (256, 256)
image.thumbnail(resize)
image.save(self.img.path)`
i tried to rewrite code 3 times, searched in the internet to solve this problem, but I can't solve it.
If you can, help me please.
答案 0 :(得分:-2)
删除form.save()
并替换为form.save(force_insert=False)