2017-02-16 99 views
2

我很难实现或使用Django过滤器的查询。我可以找到some example telling how to create OR query in django queryset。但是,我无法在django-filter文档中找到。有谁知道如何使用django-filter来实现OR查询吗? model.py和filter.py如下所示。或在Django过滤器中的查询

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'] 

回答

0

我不知道答案,但我可以建议两个途径来追求。

首先,您可以使用MultipleChoiceFilterTypedMultipleChoiceFilter? (请参阅django-filter doc https://django-filter.readthedocs.io/en/develop/ref/filters.html

其次,django-filters建议您添加自定义过滤器类型。我认为这与添加到模型管理器或查询集的自定义方法相对应。对于这些,请参阅Django文档。这是我所知道的,但从未需要去做。

我确实希望django-filters文件有点厚!