Skip to content

Instantly share code, notes, and snippets.

@JesusMurF
Last active March 22, 2017 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JesusMurF/44c4c3226745fde3d44d85f09487d6a2 to your computer and use it in GitHub Desktop.
Save JesusMurF/44c4c3226745fde3d44d85f09487d6a2 to your computer and use it in GitHub Desktop.
import time
class Offer(models.Model):
title = models.CharField(max_length=140,)
description = models.TextField(max_length=500, blank=True, null=True)
slug = models.SlugField(max_length=240, unique=True)
created_at = models.DateTimeField(auto_now_add=True, editable=False)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
new_slug = slugify(self.title)
Klass = self.__class__
if Klass.objects.filter(slug=new_slug).exists():
slug_str = '%s %s' % (self.title, str(time.time()).strip('.')[1:])
self.slug = slugify(slug_str)
else:
self.slug = new_slug
super(Offer, self).save(*args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment