Created
January 7, 2018 08:09
-
-
Save josellausas/069328672e6f784d25c0b7f7ac9b6a94 to your computer and use it in GitHub Desktop.
Django Save function for slugs
This file contains 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
def save(self, *args, **kwargs): | |
if not self.slug: | |
# Ensure uniqueness: | |
slug = slugify(self.name) | |
if not App.objects.filter(slug=slug).exists(): | |
self.slug = slug | |
else: | |
count = 1 | |
while App.objects.filter(slug=slug).exists(): | |
count += 1 | |
slug = "%s%s" % (slug, str(count)) | |
self.slug = slug |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment