Django错误:save()得到了一个意外的关键字参数'force_insert'

时间:2021-04-30 13:25:58

I have been following along with the book Practical Django Projects. Very nice book, but it seem to have a lot of bugs. Luckily this website has some fixes: https://bitbucket.org/philgyford/practical-django-projects/src. My latest error is: save() got an unexpected keyword argument 'force_insert'

我一直在关注实用Django项目。非常好的书,但似乎有很多错误。幸运的是,这个网站有一些修复:https://bitbucket.org/philgyford/practical-django-projects/src。我的最新错误是:save()得到了一个意外的关键字参数'force_insert'

The original code was:

原始代码是:

class Snippet(models.Model):
    title = models.CharField(max_length=255)
    language = models.ForeignKey(Language)
    author = models.ForeignKey(User)
    description = models.TextField()
    description_html = models.TextField(editable=False)
    code = models.TextField()
    highlighted_code = models.TextField(editable=False)
    pub_date = models.DateTimeField(editable=False)
    updated_date = models.DateTimeField(editable=False)
    objects = managers.SnippetManager()
    tags = TagField()

    class Meta:
        ordering = ['-pub_date']

    def __unicode__(self):
        return self.title

    def save(self, force_insert=False, force_update=False):
        if not self.id:
            self.pub_date = datetime.datetime.now()
        self.updated_date = datetime.datetime.now()
        self.description_html = markdown(self.description)
        self.highlighted_code = self.highlight()
        super(Snippet, self).save(force_insert, force_update)

    @models.permalink
    def get_absolute_url(self):
        return ('cab_snippet_detail', (), { 'object_id': self.id })

    def highlight(self):
        return highlight(self.code,
                         self.language.get_lexer(),
                         formatters.HtmlFormatter(linenos=True))

# See http://blog.sveri.de/index.php?/categories/2-Django
tagging.register(Snippet, tag_descriptor_attr='etags')

I changed the save method (trying to get rid of the error) to:

我将save方法(试图摆脱错误)更改为:

   def save(self, *args, **kwargs):
      if not 'force_insert' in kwargs:
         kwargs['force_insert'] = False
      if not 'force_update' in kwargs:
         kwargs['force_update'] = False
      # del kwargs['force_insert']
      if not self.id:
         self.pub_date = datetime.datetime.now()
      self.updated_date = datetime.datetime.now()
      self.description_html = markdown(self.description)
      self.highlighted_code = self.highlight()
      super(Snippet, self).save(*args, **kwargs)

I even tried uncommenting the # del bit, which also does not help. Im'm using Django 1.3.1. Any ideas why I'm getting this error?

我甚至尝试取消注释#del bit,这也没有帮助。我正在使用Django 1.3.1。任何想法为什么我收到此错误?

1 个解决方案

#1


1  

The second version of your save override should be fine, though I would get rid of the two if statements and the del line.

保存覆盖的第二个版本应该没问题,尽管我会删除两个if语句和del行。

If it still doesn't work, make sure you the development server is actually reloading the models.py file. The quickest way is to CTRL+C the running one and then call python manage.py runserver again. Although it shouldn't matter, you might want to also get rid of models.pyc as well (compiled version of models.py), just to be extra sure that the new code is running.

如果它仍然不起作用,请确保开发服务器实际上正在重新加载models.py文件。最快的方法是CTRL + C运行一个然后再次调用python manage.py runserver。虽然没关系,但你也可能想摆脱models.pyc(models.py的编译版本),只是为了确保新代码正在运行。

While Practical Django Projects is an excellent book overall, it's very outdated at this point. You might want to look for newer resources to learn from.

虽然实用Django项目总体上是一本优秀的书,但它在这一点上已经过时了。您可能希望寻找更新的资源来学习。

#1


1  

The second version of your save override should be fine, though I would get rid of the two if statements and the del line.

保存覆盖的第二个版本应该没问题,尽管我会删除两个if语句和del行。

If it still doesn't work, make sure you the development server is actually reloading the models.py file. The quickest way is to CTRL+C the running one and then call python manage.py runserver again. Although it shouldn't matter, you might want to also get rid of models.pyc as well (compiled version of models.py), just to be extra sure that the new code is running.

如果它仍然不起作用,请确保开发服务器实际上正在重新加载models.py文件。最快的方法是CTRL + C运行一个然后再次调用python manage.py runserver。虽然没关系,但你也可能想摆脱models.pyc(models.py的编译版本),只是为了确保新代码正在运行。

While Practical Django Projects is an excellent book overall, it's very outdated at this point. You might want to look for newer resources to learn from.

虽然实用Django项目总体上是一本优秀的书,但它在这一点上已经过时了。您可能希望寻找更新的资源来学习。