2
I have a manager in my project, which is to make TAGS, and on the front of my system the user will search 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 these items that have registered the overflow tag
My view filtering tags: (ta the way it was before, filtering only in 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)
Right, but then you just registered the Tags field in your templates, where you will actually include tags or even perform tag-based searches is in the view. How you’re dealing with your View ?
– Otávio Reis Perkles
No admin, when you register some of these items, already register tags tb
– Pedro Ribeiro
Yes , but to display in a view you need to 'filter' your templates based on tags and thus return a query containing the same
– Otávio Reis Perkles
Sorry, I edited with the view
– Pedro Ribeiro