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'
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:
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?
The second version of your save override should be fine, though I would get rid of the two
ifstatements and thedelline.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 runserveragain. Although it shouldn’t matter, you might want to also get rid ofmodels.pycas well (compiled version of models.py), just to be extra sure that the new code is running.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.