Skip to content

Instantly share code, notes, and snippets.

@NamPNQ
Created November 21, 2015 16:23
Show Gist options
  • Select an option

  • Save NamPNQ/17a54cb4e85add1e90f8 to your computer and use it in GitHub Desktop.

Select an option

Save NamPNQ/17a54cb4e85add1e90f8 to your computer and use it in GitHub Desktop.
def save(self, *args, **kwargs):
if not self.id:
max_length = Skill._meta.get_field('slug').max_length
self.slug = orig = slugify(self.title)[:max_length]
for x in itertools.count(1):
if not Skill.objects.filter(slug=self.slug).exists():
break
# Truncate the original slug dynamically. Minus 1 for the hyphen.
self.slug = "%s-%d" % (orig[:max_length - len(str(x)) - 1], x)
super(Skill, self).save(*args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment