I have a manager in my project, which is to do TAGS, and on the front of my system the user will search for a tag, and it will return everything related to that tag.
Follow the models.py:
from django.db import models
from django.utils import timezone
from taggit.managers import TaggableManager
class Noticia(models.Model):
id = models.BigAutoField(primary_key=True)
autor = models.ForeignKey('auth.User', on_delete=models.CASCADE)
titulo = models.CharField(max_length=255)
subtitulo = models.CharField(max_length=255)
fonte = models.CharField(max_length=255)
veiculo = models.CharField(max_length=255)
data = models.DateTimeField()
integra = models.TextField()
curtidas = models.IntegerField()
tags = TaggableManager()
def __str__(self):
return self.titulo
class Meta:
verbose_name = 'Notícia'
verbose_name_plural = 'Notícias'
# def __str__(self):
# return self.titulo
class Dados(models.Model):
dado = models.FloatField()
texto = models.CharField(max_length=255)
fonte = models.CharField(max_length=80)
anotacao = models.CharField(max_length=255, blank=True)
tags = TaggableManager()
relacionado = models.ForeignKey(Noticia, on_delete=models.CASCADE)
class Meta:
verbose_name = 'Dados da noticía'
verbose_name_plural = 'Dados das notícias'
class Case(models.Model):
titulo = models.CharField(max_length=255)
empresa = models.CharField(max_length=255)
problemas = models.TextField()
resultados = models.TextField()
tags = TaggableManager()
class Meta:
verbose_name = 'Case'
verbose_name_plural = 'Cases'
class Benchmarking(models.Model):
titulo = models.CharField(max_length=255)
empresa = models.CharField(max_length=255)
aprendizados = models.TextField()
tags = TaggableManager()
class Meta:
verbose_name = 'Benchmarking'
The TAG, is this "tags = TaggableManager ()". And I put it in several tables.
I want to type for example: Overflow, and it will return all those items that have been registered to the overflow tag
My view filtering the tags: (it's the way it used to be, filtering only on news)
def getdb(request):
noticias = Noticia.objects.all()
_tags = request.GET.get('search')
tags = ''
if _tags:
tags = _tags.split(',')
noticias = noticias.filter(tags__name__in=tags)