django-filter with django autocomplete-light

帅比萌擦擦* 提交于 2019-12-25 09:17:10

问题


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:


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


来源:https://stackoverflow.com/questions/40502794/django-filter-with-django-autocomplete-light

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!