在多个模型上使用django-filters

时间:2019-03-22 21:28:31

标签: python django python-3.x django-2.1 django-filters

我正在尝试在Django项目中创建搜索功能,该功能将过滤多个模型(当前为3个)并从调查模型返回一个值。它适用于单个模型和由外键绑定的其他模型。但是,当我为第三个模型添加一个不包含对初始模型的直接引用的下拉菜单时,我收到一个错误消息,指出他们关键字不可用(因为它不在模型中查找):

  

无法将关键字“房间”解析为字段。选项包括:DateBegin,FAN,IS,位置,PI,馆藏,id

型号:

#models.py
class rooms(models.Model):
    ContainerLocation = models.CharField(max_length=100, blank=True, null=True)
    Database = models.CharField(max_length=100, blank=True, null=True)
    Name = models.CharField(max_length=100, blank=True, null=True)
    Datatype = models.CharField(max_length=100, blank=True, null=True)

class holdings(models.Model):
    Contents = models.CharField(max_length=700, blank=True, null=True, default='No description')
    FAN = models.ForeignKey('surveys', on_delete=models.SET_NULL, blank=True, null=True)
    Database = models.ForeignKey('rooms', on_delete=models.SET_NULL, blank=True, null=True)
    ...(more fields)...

class surveys(models.Model):    
    FAN = models.SlugField(max_length=100, blank=True, null=True)
    PI = models.CharField(max_length=100, blank=True, null=True)
    IS = models.CharField(max_length=100, blank=True, null=True)
    DateBegin = models.DateField(blank=True, null=True)
    Location = models.CharField(max_length=200, blank=True, null=True)  

过滤器:

#filters.py
from django import forms
from datalibrary.models import surveys, rooms, holdings
import django_filters

class MultiFilter(django_filters.FilterSet):
    FAN = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
    PI = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
    Location = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
    Contents = django_filters.CharFilter(field_name='holdings__Contents', lookup_expr='icontains', label='Contents', distinct=True)
    Room = django_filters.ModelChoiceFilter(queryset=rooms.objects.all(), label='Room', distinct=True)

    class Meta:
        model = surveys
        fields = ['FAN', 'PI', 'Location', 'Contents', 'Room']

观看次数:

#views.py
def search(request):
    multifilter = MultiFilter(request.GET, queryset=surveys.objects.all())
    return render(request, 'search_results.html', {'filter': multifilter})

是否可以使用搜索多个模型的django-filters构建过滤器?我可以创建合并的查询集或类似的东西吗?我尝试将列表添加到过滤器Meta(例如model = [surveys, rooms, holdings])中,但这显然不起作用。

如果django-filters无法做到这一点,是否还有其他选项可用于在Django中搜索多个模型?

1 个答案:

答案 0 :(得分:0)

我弄清楚了它应该如何工作。可以使用modelname__field指定另一个模型(两个下划线)。所以看起来像这样:

#filters.py
from django import forms
from datalibrary.models import surveys, rooms, holdings
import django_filters

class MultiFilter(django_filters.FilterSet):
    FAN = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
    PI = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
    Location = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
    Contents = django_filters.CharFilter(field_name='holdings__Contents', lookup_expr='icontains', label='Contents', distinct=True)
    Room = django_filters.ModelChoiceFilter(lookup_expr='icontains', label='Room', distinct=True)

    class Meta:
        model = surveys
        fields = ['FAN', 'PI', 'Location', 'Contents', 'rooms__name']

请注意,已将Room更改为rooms_name