2

I am having difficulty to implement OR query using django-filter. I could find some example telling how to create OR query in django queryset.

But, I could not find in django-filter documentation. Does anyone know how to implement OR query using django-filter? model.py and filter.py is shown below.

model.py

class html(models.Model):
    project = models.CharField(max_length=50, blank=True)
    version = models.IntegerField(default=0)
    ecms=models.ManyToManyField(ecm, blank=True)
    diff = models.TextField(blank=True)
    PROGRAM_CHOICES = (
        ('Office', 'General office'),
        ('Residential', 'Residential'),
        ('Retail', 'Retail'),
        ('Restaurant', 'Restaurant'),
        ('Grocery', 'Grocery store'),
        ('Medilcal', 'Medilcal office'),
        ('Research', 'R&D or laboratory'),
        ('Hotel', 'Hotel'),
        ('Daycare', 'Daycare'),
        ('K-12', 'Educational,K-12'),
        ('Postsecondary', 'Educational,postsecondary'),
        ('Airport', 'Airport'),
        ('DataCenter','Data Center'),
        ('DistributionCenter','Distribution center,warehouse')
    )
    program = models.CharField(max_length=20, choices=PROGRAM_CHOICES, default='Retail')
    LOCATION_CHOICES = (
        ('Beijing', 'Beijing'),
        ('China', 'China'),
        ('Hong Kong', 'Hong Kong'),
        ('Japan', 'Japan'),
        ('Shanghai', 'Shanghai'),
        ('Shenzhen', 'Shenzhen'),
        ('Taiwan', 'Taiwan')
    )
    location = models.CharField(max_length=15, choices=LOCATION_CHOICES, default="Hong Kong")
    CERTIFICATE_CHOICES = (
        ('LEED_v3', 'LEED_v3'),
        ('LEED_v4', 'LEED_v4'),
        ('BEAM+', 'BEAM+'),
        ('WELL', 'WELL')
    )
    certificate = models.CharField(max_length=10, choices=CERTIFICATE_CHOICES, default='BEAM+')
    user = models.CharField(max_length=20, default='test')
    html = models.FileField(upload_to=dir_path)
    uploaded_at = models.DateTimeField(auto_now_add=True)

filter.py

import django_filters
from .models import html

class ProjectFilter(django_filters.FilterSet):
    class Meta:
        model=html
        fields=['project','program','location','certificate','user']
markwalker_
  • 12,078
  • 7
  • 62
  • 99
Katsuya Obara
  • 903
  • 3
  • 14
  • 30

2 Answers2

0

I don't know the answer, but I can suggest two avenues to pursue.

Firstly, can you use a MultipleChoiceFilter or TypedMultipleChoiceFilter? (see django-filter doc https://django-filter.readthedocs.io/en/develop/ref/filters.html)

secondly, django-filters suggests that you can add a custom filter type. I presume this corresponds to a custom method added to your model's manager or queryset. For these, consult the Django documentation. It's something I know of, but have never yet needed to do.

I do wish the django-filters documentation was a bit thicker!

nigel222
  • 7,582
  • 1
  • 14
  • 22
0

You can make complex query sets with the Q object, maybe this is what you need

https://docs.djangoproject.com/en/1.10/topics/db/queries/#complex-lookups-with-q-objects

Tobit
  • 406
  • 7
  • 19