Skip to content Skip to sidebar Skip to footer

Why Won't This Override Of The Model.save() Function In Django Work?

I am new to SO and Python/Django, so please bear with me. In my generic blog app based on the tutorial at http://www.djangoproject.com/, I am trying to create slugs for posts when

Solution 1:

modocache,

What version of django are you using? What you have there listed should work, I use that same logic in many of my own models, and it works fine.

According to this page: http://fosshelp.blogspot.com/2010/12/django-override-save-method-two-ways.html

you should be able to change the code to look like this (below), and it will do the same thing but won't reference the Post model.

defsave(self, *args, **kwargs):
    ifnot self.id:
        self.slug = slugify( self.title )
    models.Model.save(self, *args, **kwargs ) # <-- notice the self

Another point , instead of using "if not self.id:" it is generally better practice to use "if not self.pk:" instead. see these related links.

Django queries - id vs pk

http://docs.djangoproject.com/en/dev/ref/models/instances/#the-pk-property

How that helps.

Solution 2:

I'm wondering if you have an indentation error at your super() line -- do you have tabs and spaces mixed up?

Upon starting the server even typing in super(IDONTEXIST, self) should not throw an error until save() is called.

I can reproduce your error if I de-indent the super line.

Post a Comment for "Why Won't This Override Of The Model.save() Function In Django Work?"