Searching for a field contained in several models

Asked

Viewed 68 times

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 ?

  • No admin, when you register some of these items, already register tags tb

  • Yes , but to display in a view you need to 'filter' your templates based on tags and thus return a query containing the same

  • Sorry, I edited with the view

1 answer

1


To be able to filter efficiently you have to pass the resulting object of your search as context to your view.

py views.

def getdb(request):
    _tags = request.GET.get('search')
    if _tags:
        tags = _tags.split(',')

    noticias = Noticia.objects.all().filter(tags__name__in=[_tag])
    #Pesquisa todos os objetos Noticia que contem sua tag

    context = {
        'noticias': noticias,
    }

    return render(request, 'seuhtml.html', context)

Finally just display your resulting object in your template

seuhtml.html

{% for item in noticias %}
    {{item.autor}}
    {{item.titulo}}
    {{item.subtitulo}}
    ...
{% endfor %}

To filter through all the objects in your model you need to maintain a relationship between them. As in your objteto dados, since the relationship exists relacionado = models.ForeignKey(Noticia, on_delete=models.CASCADE) the same becomes available in your view:

{% for item in noticias %}
    {{item.autor}}
    {{item.titulo}}
    {{item.subtitulo}}
    {{item.dados.texto}}
    {{item.dados.fonte}}
    ...
{% endfor %}

Establish the proper relationship between the models you want to filter and they will become available by your main query:

noticias = Noticia.objects.all().filter(tags__name__in=[_tag]).

Note that once relationships are well established You only need to have a tag in your news (tags = TaggableManager()) since it is only for her that will be based to obtain the rest.

For more information about relationships visit.

For more information about the tags and the bilioteca Voce uses access.

  • but I need to search for more items besides news

  • Right , added the changes

  • the search ta returning empty look only: <Queryset []>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.