How do I apply a python/Django filter

Asked

Viewed 146 times

-2

from django_filters import FilterSet
from django.db import models

Pessoa(models.Model):
  nome= models.TextField()

Marca(models.Model):
  nome = models.TextField()

Carro(models.Model):
  pessoa = models.ForeignKey(Pessoa, on_delete=models.CASCADE, related_name='carros')
  marca = models.ForeignKey(Marca, on_delete=models.CASCADE)
  data_do_documento = models.DateTimeField()

class FilterPessoa(FilterSet):
  ...
  class Meta:
     model = Pessoa
     ...
    

How do I apply a python/Django filter, of all people who have fiat and/or jeep cars, and have document date between 01/07/2020 and 31/07/2020

2 answers

0

Now I understood then just follow this example

class FilterPessoa(FilterSet):
  pessoa = django_filters.CharFilter(lookup_expr='icontains')
  class Meta:
     model = Carro
     fields ={ 'pessoa': [exact, 'marca'],
             }

In this example will make you bring people by vehicle brands.

0

In the logic you’re using.

This class is used as follows.

class FilterPessoa(FilterSet):
  pessoa__nome = django_filters.CharFilter(lookup_expr='icontains')
  class Meta:
     model = Carro
     fields = ['pessoa__nome']

I believe your code needs a logic review.

  • Hey there, Jose Augusto. That would not be the problem because the filter I want is for all the people who have Fiat and Jeep cars, regardless of the name of the person. However thank you very much.

Browser other questions tagged

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