Django - TypeError - save() got an unexpected keyword argument 'force_insert'

前端 未结 4 662
感动是毒
感动是毒 2020-12-03 04:41

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

相关标签:
4条回答
  • 2020-12-03 05:15

    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)'
    
    0 讨论(0)
  • 2020-12-03 05:16

    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)
    
    0 讨论(0)
  • 2020-12-03 05:40

    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)
        ...
    
    0 讨论(0)
  • 2020-12-03 05:42

    Python 3 version

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

    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.

    0 讨论(0)
提交回复
热议问题