Automate unique slugs
If you want unique values for a slug field, but don't want to bother the user with error messages, this function can be put into a model's save function to automate unique slugs. It works by appending an integer counter to duplicate slugs. The item's slug field is first prepopulated by slugify-ing the source field. If that value already exists, a counter is appended to the slug, and the counter incremented upward until the value is unique. For instance, if you save an object titled Daily Roundup, and the slug daily-roundup is already taken, this function will try daily-roundup-2, daily-roundup-3, daily-roundup-4, etc, until a unique value is found. Call from within a model's custom save() method like so: `unique_slug(item, slug_source='field1', slug_field='field2')` where the value of field slug_source will be used to prepopulate the value of slug_field. Comments appreciated!
- slug
- save