Skip to content

Instantly share code, notes, and snippets.

@ceolson01
Last active August 23, 2016 22:12
Show Gist options
  • Save ceolson01/cf615e958a9a60828ff8 to your computer and use it in GitHub Desktop.
Save ceolson01/cf615e958a9a60828ff8 to your computer and use it in GitHub Desktop.
Django Simple Slugify Method
from django.utils.text import slugify
def save(self, *args, **kwargs):
if not self.slug:
potential_slug = orig = slugify(self.title)
if Story.objects.filter(slug=potential_slug).exists():
slug_counter = 1
while Story.objects.filter(slug=potential_slug).exists():
potential_slug = '%s-%d' % (orig, slug_counter)
slug_counter += 1
self.slug = potential_slug
super(Story, self).save(*args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment