redundant slug definition?

So slug is defined in

def save(self, *args, **kwargs): if not self.slug: self.slug = slugify(self.title) super(Post, self).save(*args, **kwargs)

and also

prepopulated_fields = {"slug": ("title",)}

It feels redundant to define same thing in two places..

Sorry You must be a logged in, registered user to answer a question.

Answers

It is not redundant, although it may seem that way. prepopulated_fields is a Django admin only setting. If you are using the Django admin to create a blog post, this setting activates a bit of Javascript that automatically populates a field based on another field.

From the Django docs on prepopulated_fields:

When set, the given fields will use a bit of JavaScript to populate from the fields assigned. The main use for this functionality is to automatically generate the value for SlugField fields from one or more other fields. The generated value is produced by concatenating the values of the source fields, and then by transforming that result into a valid slug (e.g. substituting dashes for spaces).

This is simply a convenience to have the slug generated automatically for any user using the Django admin. The user creating the blog post can still edit the slug before submitting if they wish, they could even delete it completely. Since the URL for the blog post detail page requires a slug to be present, overriding the save method and making sure one is always set is your fail safe.