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


I'm new to SO and Python / Django, so please support me.

The blog app based on the tutorial in my generic, I am trying to create slugs for posts when they are saved in the database using the slugify () method on the title of the post here. Models.py: django.db import model from django.template.defaultfilters import slugify import datetime class post (models.Model): DEF __unicode __ (self): return self. Title Title = Model. Richfield (max_long = 200) slug = model. Sloughfind (editable = false) body = model TextField () pub_date = Model. Datatiffild ('Date published') Def Safe (self, * args, ** Kwargs): If not self.id: self.slug = slugify (self.title) super (post, self). Unfortunately, it throws up the following exception when trying to start the server:

  file "/ user Name / post / models / "/ code / blog / post / models", line 24, post super (post, self) .ave (* args, ** kwargs) NameError: name 'post' is not defined   

Why I am confused why NameError has been thrown. I thought I was misusing the Super () method, but it works, in spite of this I'm looking like the same thing: class Foo (object): def Say_spam (self): Print "Spam!" Square Bar (Foo): SPAM (self) to say DF: Print "brought by you:" Super (bar, self). Say_spam () Print "Egg!" B = bar () b.say_spam ()

So if this works, then why does the above Django snippet fail? It is especially puzzling that according to the documentation of djangoproject.com this should work:

  # http://docs.djangoproject.com/en/dev/topics/db/ Models / # overriding-model-methods class blog (models.Model): name = models.CharField (max_length = 100) tagline = models.TextField () def save (self, * args, ** kwargs): do_something () super (Blog, self). (* Args, ** Quad) # Call the "real" save () method.   

Thanks in advance for any and all help, I really appreciate it!

Modacach,

Which version of the defective are you using? What you need to be listed, should work, I use the same logic in many of my own models, and it works fine.

According to this page:

You should be able to change the code for viewing this (below), and it will do the same thing, but will not reference the post model. def save (self, * args, ** kwargs): if not self.id: self.slug = slugify (self.title) models.Model.save (self, * args, ** kwargs) # & lt; Instead of using self.id, another issue, "It is usually better practice" if self is not PK: "See these related links instead"

How it helps.

Comments