Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 443 Vote(s) - 3.58 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Django - TypeError - save() got an unexpected keyword argument 'force_insert'

#1
Im new in Django and I can't figure out this error. Help please. It gave TypeError - save() got an unexpected keyword argument 'force_insert'. I tested the code below and they were able to save the new user registration but now it won't save anymore...

Here is part of the views.py that i think got some problem:


from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from . forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm

def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
form.save(force_insert=False)
messages.success(request, f'Thank you {username}! Your account has been created!')
return redirect('login')
else:
form = UserRegisterForm()

return render(request, 'users/register.html', {'form':form})

and the 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)
image = models.ImageField(default='profile_pics/default.jpg', upload_to='profile_pics')

def __str__(self):
return (self.user)

def save(self):
super().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)'

Reply

#2
You've overridden the save method, but you haven't preserved its signature. Yo need to accept the same arguments as the original method, and pass them in when calling super.

def save(self, *args, **kwargs):
super().save((*args, **kwargs)
...
Reply

#3
When you are overriding model's save method in Django, you should also pass `*args` and `**kwargs` to overridden method. this code may work fine:

def save(self, *args, **kwargs):
super(Profile, self).save(*args, **kwargs)

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)'
Reply

#4
I had the same problem.

This will fix it:

Edit the super method in your *users/models.py* file:

def save(self, *args, **kwargs):
super.save(*args, **kwargs)
Reply

#5
## Python 3 version

I was looking for this error on Google and found this topic. This code solved my problem in Python 3:

```python
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
super().save(force_insert, force_update, using, update_fields)
```

* Tested with `Django 3.0` and `Python 3.8`.

[Django `models.Model` source code][1].


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through