Last active
March 22, 2017 12:00
-
-
Save JesusMurF/44c4c3226745fde3d44d85f09487d6a2 to your computer and use it in GitHub Desktop.
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
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