django过滤器与django自动完成光 [英] django-filter with django autocomplete light

查看:495
本文介绍了django过滤器与django自动完成光的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人成功地将dal和django过滤器一起使用?
以下尝试是我的,
我试图使用filterset_factory,提供模型类和字段列表,然后我尝试使用futuremodelform。
我得到,



ModelForm没有指定模型类。



我认为这只是许多错误发生。
任何人以前做过,我必须使用filterset_factory,并从参数创建动态类,我也想覆盖小部件,以便dal小部件可以使用。

  #testing filterset 
from dal import autocomplete
from django.db import models
class PanFilterSet(django_filters.FilterSet):
filter_overrides = {
models.ForeignKey:{
'filter_class':autocomplete.ModelSelect2,
},

}

def pan_filterset_factory(model,fields) :
meta = type(str('Meta'),(object,),{'model':model,'fields':fields,'form':autocomplete.FutureModelForm})
filterset = type (str('%sFilterSet'%model._meta.object_name),
(PanFilterSet,),{'Meta':meta})
返回filterset

searchFormFilterSet = pan_filterset_factory self.model_class,self.final_search_fields)
f = searchFormFilterSet(self.request.GET,queryset = self.get_queryset())
print f.form.as_p()


解决方案

我对DAL不太熟悉,但是我对django过滤器做出了贡献,并对其内部结构有了一个很好的理解。一些注释:




  • filter_overrides中的 filter_class 应该是一个过滤器,而不是一个小部件。您可以通过 extra 键提供其他参数(如小部件),如 here 。不属于过滤器的任何参数都将自动传递到基础表单域。

  • 无论如何,使用覆盖不是正确的方法,因为窗口小部件需要一个字段特定的端点执行自动完成。由于端点是字段特定的,它不适用于所有 ForeignKey

  • django-filter使用常规 Form s,而不是 ModelForm s,所以适当的 Meta 内部类不会建。 FutureModelForm 似乎没有提供自动完成功能 - 似乎无关紧要?



您的工厂必须手动生成自动完成过滤器,如下所示:

  def dal_field (field_name,url):
return filters.ModelChoiceFilter(
name = field_name,
widget = autocomplete.ModelSelect2(url = url),


def dal_filterset_factory(model,fields,dal_fields):
attrs = {field:dal_field(field,url)for field,url in dal_fields.items()}
attrs ['Meta'] = type str('Meta'),(object,),{'model':model,'fields':fields})

filterset = type(str('%sFilterSet'%model._meta.object_name ),
(FilterSet,),attrs)
返回filterset

#用法:

#映射{field names:autocomplete endpoints}。
dal_fields = {'birth_country':'country-autocomplete'}
fields = ['list','或','dict','of','other','fields']
SomeModelFilterSet = dal_filterset_factory(SomeModel,fields,dal_fields)

attrs 使用声明式API。 文档中的更多信息。


Has anyone succesfully used dal and django-filter together? Below attempt is mine, I tried to use filterset_factory, supplying model class and fields list, then I tried to use futuremodelform. I got ,

ModelForm has no model class specified.

I think it's just one of many errors to occur. Anybody done that before, I have to use filterset_factory, and create dynamic classes from arguments, I also want to override widgets so dal widgets can be used.

   #testing filterset
    from dal import autocomplete
    from django.db import models
    class PanFilterSet(django_filters.FilterSet):
        filter_overrides = {
            models.ForeignKey: {
                'filter_class': autocomplete.ModelSelect2,
            },

        }

    def pan_filterset_factory(model,fields):
        meta = type(str('Meta'), (object,), {'model': model,'fields':fields,'form':autocomplete.FutureModelForm})
        filterset = type(str('%sFilterSet' % model._meta.object_name),
                         (PanFilterSet,), {'Meta': meta})
        return filterset

    searchFormFilterSet = pan_filterset_factory(self.model_class,self.final_search_fields)
    f = searchFormFilterSet(self.request.GET, queryset=self.get_queryset())
    print f.form.as_p()

解决方案

I'm not very familiar with DAL, but I contribute to django-filter and have a decent understanding of its internals. A few notes:

  • The filter_class in your filter_overrides should be a filter, not a widget. You can provide additional arguments (such as the widget) through the extra key, as seen here. Any parameter that does not belong to the filter is automatically passed to the underlying form field.
  • Using an override isn't the right approach anyway, as the widget needs a field-specific endpoint to perform autocompletion. Since the endpoint is field-specific, it's not applicable to all ForeignKeys.
  • django-filter uses regular Forms, not ModelForms, so an appropriate Meta inner class would not be constructed. FutureModelForm doesn't seem to provide autocomplete functionality anyway - it seems irrelevant?

Your factory will have to generate your autocomplete filters manually - something like the following:

def dal_field(field_name, url):
    return filters.ModelChoiceFilter(
        name=field_name,
        widget=autocomplete.ModelSelect2(url=url),
    )

def dal_filterset_factory(model, fields, dal_fields):
    attrs = {field: dal_field(field, url) for field, url in dal_fields.items()}
    attrs['Meta'] = type(str('Meta'), (object,), {'model': model,'fields': fields})

    filterset = type(str('%sFilterSet' % model._meta.object_name),
                     (FilterSet,), attrs)
    return filterset

# Usage:

# mapping of {field names: autocomplete endpoints}.
dal_fields = {'birth_country': 'country-autocomplete'}
fields = ['list', 'or', 'dict', 'of', 'other', 'fields']
SomeModelFilterSet = dal_filterset_factory(SomeModel, fields, dal_fields)

The fields in attrs use the declarative API. More info in the docs.

这篇关于django过滤器与django自动完成光的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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