Last active
August 23, 2016 22:12
-
-
Save ceolson01/cf615e958a9a60828ff8 to your computer and use it in GitHub Desktop.
Django Simple Slugify Method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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