Last active
December 17, 2015 11:48
-
-
Save JuniorLima/5604528 to your computer and use it in GitHub Desktop.
A notícia tem várias categorias. Suponto que temos duas categorias cadastradas: 'Notícias' e 'Eventos'. Se a notícia tiver categoria 'Noticia', ele salva o 'id' mais o 'slugnoticia' da noticia no banco. Se ela for um evento, salva somente a 'slugnoticia'
This file contains hidden or 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
class Categoria(models.Model): | |
nome = models.CharField(max_length=30) | |
slugcategoria = models.SlugField(max_length=100, blank=True, unique=True) | |
class Noticia(BaseNoticia): | |
titulo = models.CharField(max_length=100, unique=True) | |
slugnoticia = models.SlugField(max_length=200, blank=True, unique=True) | |
def noticia_pre_save(signal, instance, sender, **kwargs): | |
if instance.nomeCategoria.id == '1': | |
pegaid = slugify(instance.id) | |
pegatitulo = slugify(instance.titulo) | |
novaslug = '%s-$s'%(pegaid, pegatitulo) | |
else: | |
semid = slugify(instance.titulo) | |
novaslug = semid | |
instance.slugnoticia = novaslug | |
signals.pre_save.connect(noticia_pre_save, sender=Noticia) |
This file contains hidden or 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
class Categoria(models.Model): | |
nome = models.CharField(max_length=30) | |
slugcategoria = models.SlugField(max_length=100, blank=True, unique=True) | |
class Noticia(BaseNoticia): | |
titulo = models.CharField(max_length=100, unique=True) | |
slugnoticia = models.SlugField(max_length=200, blank=True, unique=True) | |
def noticia_pre_save(signal, instance, sender, **kwargs): | |
""" | |
Estamos gerando uma nova slug para evitar duplicidade | |
""" | |
if not instance.nomeCategoria_id == 1: | |
slug = slugify(instance.titulo) | |
novaslug = slug | |
contador = 0 | |
else: | |
contador = slugify(instance.id) | |
slug = slugify(instance.titulo) | |
novaslug = '%s-%s'%(slug, contador) | |
instance.slugnoticia = novaslug | |
signals.pre_save.connect(noticia_pre_save, sender=Noticia) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment