V
V
Vlad2422019-02-24 17:39:36
Django
Vlad242, 2019-02-24 17:39:36

How to solve such error in django?

What needs to be changed in the code?

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()

Here is the TypeError: save() got an unexpected keyword argument 'force_insert'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-02-24
@Vlad242

You lose the save method arguments. Fix like this:

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

Better yet, do not redefine it at all and shift the task of resizing the avatar to the signal handlersave_profile

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question