在使用 Django2.0 版本的 Django Rest Framwork 时,Django DeBug 报错


django-filter: TypeError at *** __init__() got an unexpected keyword argument 'name'


百度无效,于是查阅官方文档。 

在 官方文档  中,是这样写的


To alter strictness behavior, the appropriate view code should be overridden. More details will be provided in future docs.

​Filter.name​​​ renamed to ​​Filter.field_name​​​ (​​#792​​)

The filter ​​name​​​ has been renamed to ​​field_name​​​ as a way to disambiguate the filter’s attribute name on its FilterSet class from the ​​field_name​​ used for filtering purposes.


 所以我们代码中的 name 要改为 field_name:

price_min = django_filters.NumberFilter(name='shop_price', lookup_expr='gte')
price_max = django_filters.NumberFilter(name='shop_price', lookup_expr='lte')


    price_min = django_filters.NumberFilter(field_name='shop_price', lookup_expr='gte')
price_max = django_filters.NumberFilter(field_name='shop_price', lookup_expr='lte')

再调试一下即可成功 

小结:对于部分百度不到的错误,不要害怕看官方文档,官方文档往往是最权威的。