2

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.

cezar
  • 11,616
  • 6
  • 48
  • 84
rifaiz
  • 39
  • 2
  • 9

1 Answers1

7

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']
Sherpa
  • 1,948
  • 13
  • 25
  • Thanks for helping me understand. I did place the widget parameter in the form class, but your example works. – rifaiz Nov 11 '16 at 10:01
  • @Sherpa I implemented it just like this, but I get an empty select box instead of an autocomplete box. Did you do anything special in the template? – OverflowingTheGlass Sep 08 '17 at 11:10
  • 1
    @CameronTaylor you may need to include the form media in your template. `{{ filter.form.media }}` – Sherpa Sep 11 '17 at 08:17