[Django]-Why won't this override of the Model.save() function in Django work?

4đź‘Ť

âś…

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.

 def save(self, *args, **kwargs):
    if not 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.

👤Ken Cochrane

1đź‘Ť

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.

Leave a comment