django-filter with django autocomplete-light

2019-09-15 18:31发布

I've been using simple DAL, and django-filter separately but I'm having trouble using DAL with django-filter.

I've read this page : django-filter with django autocomplete light

but I'm still confused. I have filter class like this below, and I want to use DAL on the "devname" field:

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

any help or point-to-right-direction please.

1条回答
劫难
2楼-- · 2019-09-15 18:41

Filters are just an abstraction on top of regular Django form fields. Any arguments that do not apply to the filter are passed to the underlying field. In this case, all you need to do is hook up the autocomplete widget with the filter. Probably something like:

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']
查看更多
登录 后发表回答