Just finished a website using Django and i got stuck after creating a user or superuser.
For some reason the same code i used before does not work anymore, and now whenever i create a new user it is saved (because i cant create another one with the same name) but not a profile for it.
So now, after the registration form, the user should be redirected to the profile page which brings back an error. If i try to restart the server and login again, same error appears.
Here is my signals.py
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from .models import Profile
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
and 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='default.jpg', upload_to='profile_pics')
def __str__(self):
return f'{self.user.username} Profile'
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)
TypeError at /register/
save() got an unexpected keyword argument 'force_insert'
You need to update the save
method to match its original function signature
. Basically you need to pass arguments and keyword arguments through super function to make it work:
class Profile(models.Model):
# rest of the code
def save(self, *args, **kwargs):
super().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)