django-filter with django autocomplete-light

时间:2016-11-09 08:42:21

标签: django django-filter django-autocomplete-light

我一直在使用简单的DAL和django-filter,但我在使用django-filter时遇到了麻烦。

我已阅读此页:django-filter with django autocomplete light

但我还是很困惑。 我有如下所示的过滤器类,我想在" devname"上使用DAL。字段:

class DevListFil(django_filters.FilterSet):
    devname = django_filters.CharFilter(name='devname',lookup_expr='icontains')
    sn      = django_filters.CharFilter(name='sn',lookup_expr='icontains')
    devtype = django_filters.CharFilter(name='devtype',lookup_expr='icontains')
    class Meta:
        model = Device
        fields = ['devname','sn','devtype']

请提供任何帮助或从右到右的方向。

1 个答案:

答案 0 :(得分:2)

过滤器只是常规Django表单字段之上的抽象。任何不适用于过滤器的参数都将传递给基础字段。在这种情况下,您需要做的就是将自动完成小部件与过滤器连接起来。可能类似于:

devname_url = '...'

class DevListFil(django_filters.FilterSet):
    devname = django_filters.CharFilter(name='devname', lookup_expr='icontains', widget=autocomplete.ModelSelect2(url=devname_url))
    sn      = django_filters.CharFilter(name='sn', lookup_expr='icontains')
    devtype = django_filters.CharFilter(name='devtype', lookup_expr='icontains')

    class Meta:
        model = Device
        fields = ['devname', 'sn', 'devtype']