Search code examples
djangosignals

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


I'm trying to implement a signal that creates a Profile upon the save of a User instance.

Trying to test this out with my form to register users gives me the following stacktrace:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/auth/register/

Django Version: 3.2.4
Python Version: 3.8.10
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django_extensions',
 'webpack_loader',
 'home',
 'personal_list',
 'utils',
 'authentication']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/home/noxlock/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/noxlock/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/noxlock/my-anime-openings-list/MAOL/authentication/views.py", line 12, in register
    form.save()
  File "/home/noxlock/.local/lib/python3.8/site-packages/django/contrib/auth/forms.py", line 131, in save
    user.save()
  File "/home/noxlock/.local/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 67, in save
    super().save(*args, **kwargs)
  File "/home/noxlock/.local/lib/python3.8/site-packages/django/db/models/base.py", line 726, in save
    self.save_base(using=using, force_insert=force_insert,
  File "/home/noxlock/.local/lib/python3.8/site-packages/django/db/models/base.py", line 774, in save_base
    post_save.send(
  File "/home/noxlock/.local/lib/python3.8/site-packages/django/dispatch/dispatcher.py", line 180, in send
    return [
  File "/home/noxlock/.local/lib/python3.8/site-packages/django/dispatch/dispatcher.py", line 181, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "/home/noxlock/my-anime-openings-list/MAOL/personal_list/models.py", line 45, in create_profile
    Profile.objects.create(user=instance)
  File "/home/noxlock/.local/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/noxlock/.local/lib/python3.8/site-packages/django/db/models/query.py", line 453, in create
    obj.save(force_insert=True, using=self.db)

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

Typically this is usually due to missing **kwargs in the definition of a save method, but I actually DON'T have one written for my Profile model, and I'm using the standard Django User model, for which this is the only extension I have written.

Below is my Profile model and signal code.

from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User

from imagekit.models import ProcessedImageField
from imagekit.processors import ResizeToFill

from home.models import ModelAbstract


# P.S ModelAbstract doesn't have a save method either, just adds some nifty date fields.
class Profile(ModelAbstract):

    user = models.OneToOneField(User, on_delete=models.CASCADE)
    avatar = ProcessedImageField(
        upload_to='avatars', default='avatars/placeholder.png',
        processors=[ResizeToFill(256, 256)],
        format="PNG"
    )
    banner = ProcessedImageField(
        upload_to='banners', default='banners/placeholder.png',
        processors=[ResizeToFill(256, 256)],
        format="PNG"
    )

    def get_recent_ratings(self):
        pass

    def get_top_ratings(self):
        pass

# On User creation, make a profile as well
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

Solution

  • Guess what, I actually DID have a problem with missing *args and **kwargs in my save method. Just in the parent model that Profile was inherting from.

    Here's what the code looked like:

    class DateData(models.Model):
    
    
        date_created = models.DateTimeField()
        last_modified = models.DateTimeField()
    
        class Meta:
            abstract = True
    
        def save(self):
            # Only set this field on the first save
            if not self.date_created:
                date_created = timezone.now()
            last_modified = timezone.now()
    
    
    
    class ModelAbstract(DateData):
    
    
        class Meta:
            abstract = True
    

    Just needed to change that to def save(self, *args, **kwargs):