Skip to content

Instantly share code, notes, and snippets.

@josellausas
Created January 7, 2018 08:09
Show Gist options
  • Save josellausas/069328672e6f784d25c0b7f7ac9b6a94 to your computer and use it in GitHub Desktop.
Save josellausas/069328672e6f784d25c0b7f7ac9b6a94 to your computer and use it in GitHub Desktop.
Django Save function for slugs
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