带有django autocomplete-light的django-filter [英] django-filter with django autocomplete-light

查看:79
本文介绍了带有django autocomplete-light的django-filter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在分别使用简单的DAL和django-filter,但是在将DAL与django-filter结合使用时遇到麻烦.

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

我已阅读此页面:带有django自动补全灯的django过滤器

但是我还是很困惑. 我下面有这样的过滤器类,我想在"devname"字段上使用DAL:

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.

推荐答案

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

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

这篇关于带有django autocomplete-light的django-filter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆