django-filter version 2.0 改动
今天使用django-filter时候遇到了下面这个问题:
django-filter: TypeError at /goods/ init() got an unexpected keyword argument 'name'
I am learning Django REST Framework. And I want to use the django-filter in django rest framework to choose the result I want. I wrote a filter.py to get goods whose price is between pricemin and pricemax:
class GoodsFilter(filters.FilterSet):
pricemin = filters.NumberFilter(name="shop_price", lookup_expr='gte')
pricemax = filters.NumberFilter(name="shop_price", lookup_expr='lte')
class Meta:
model = Goods
fields = ['pricemin', 'pricemax']
then I used this filter in view.py:
class GoodsListViewSet(mixins.ListModelMixin,viewsets.GenericViewSet):
queryset = Goods.objects.all()
serializer_class = GoodsSerializer
pagination_class = GoodsPagination
filter_backends = (DjangoFilterBackend,)
filter_class = GoodsFilter
when I run my project, there is an error like this:
TypeError at /goods/
__init__() got an unexpected keyword argument 'name'
I did not use argument 'name' in my code, so I have no idea about this error. Traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/goods/
Django Version: 2.0.3
Python Version: 3.6.4
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'DjangoUeditor',
'users.apps.UsersConfig',
'goods.apps.GoodsConfig',
'trade.apps.TradeConfig',
'user_operation.apps.UserOperationConfig',
'crispy_forms',
'xadmin',
'rest_framework',
'django_filters']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\rest_framework\viewsets.py" in view
103. return self.dispatch(request, *args, **kwargs)
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\rest_framework\views.py" in dispatch
483. response = self.handle_exception(exc)
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\rest_framework\views.py" in handle_exception
443. self.raise_uncaught_exception(exc)
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\rest_framework\views.py" in dispatch
480. response = handler(request, *args, **kwargs)
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\rest_framework\mixins.py" in list
40. queryset = self.filter_queryset(self.get_queryset())
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\rest_framework\generics.py" in filter_queryset
152. queryset = backend().filter_queryset(self.request, queryset, self)
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\django_filters\rest_framework\backends.py" in filter_queryset
94. if not filterset.is_valid() and self.raise_exception:
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\django_filters\filterset.py" in is_valid
206. return self.is_bound and self.form.is_valid()
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\django_filters\rest_framework\filterset.py" in form
24. form = super().form
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\django_filters\filterset.py" in form
258. Form = self.get_form_class()
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\django_filters\filterset.py" in get_form_class
250. for name, filter_ in self.filters.items()])
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\django_filters\filterset.py" in <listcomp>
250. for name, filter_ in self.filters.items()])
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\django_filters\filters.py" in field
138. self._field = self.field_class(label=self.label, **field_kwargs)
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\django\forms\fields.py" in __init__
330. super().__init__(max_value=max_value, min_value=min_value, **kwargs)
File "C:\Users\Harry\Envs\testvir2\lib\site-packages\django\forms\fields.py" in __init__
254. super().__init__(**kwargs)
Exception Type: TypeError at /goods/
Exception Value: __init__() got an unexpected keyword argument 'name'
原因如下
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 thefield_name
used for filtering purposes.
So, from django-filter==2.0 onwards, use field_name instead of name
class GoodsFilter(filters.FilterSet):
pricemin = filters.NumberFilter(field_name="shop_price", lookup_expr='gte')
pricemax = filters.NumberFilter(field_name="shop_price", lookup_expr='lte')
class Meta:
model = Goods
fields = ['pricemin', 'pricemax']
总结:
遇到问题,先查阅官方文档最靠谱,问谁都白搭~~
django-filter version 2.0 改动的更多相关文章
- Tomcat - java.lang.UnsupportedClassVersionError:Unsupported major.minor version 51.0 (unable to load class com.microsoft.sqlserver.jdbc.SQLS
今天使用Tomcat连接sql Server 2008 enterprise的时候,报错: HTTP Status 500 - Servlet execution threw an exception ...
- 【边做项目边学Android】小白会遇到的问题--This Android SDK requires Android Developer Toolkit version 23.0.0 or above
问题描写叙述: 上一篇讲到解决Appcompat_V7问题要减少adt版本号,于是就换旧版本号22.3.0啊,又一次打开Eclipse.立刻弹出: This Android SDK requires ...
- python2.X现在不能安装Django了:Collecting django Using cached Django-2.0.tar.gz
使用pip安装django2: pip install django 报错: Collecting django Using cached Django-2.0.tar.gz Complete ...
- 简单的横向ListView实现(version 3.0)
版本号2仅仅是简单的实现了当手指按下的时候listView的Item向左移动一定的距离,并没有随着手指的左右移动而左右滚动.在这个版本号3.0中将会实现随着手指的移动而滚动的目标:当手指向左移动的时候 ...
- Element filtername is not allowed here-web.xml version="3.0"-intellij idea
Element filtername is not allowed here-web.xml version="3.0"-intellij idea Intellij IDEA 报 ...
- ASP.NET Core: You must add a reference to assembly mscorlib, version=4.0.0.0
ASP.NET Core 引用外部程序包的时候,有时会出现下面的错误: The type 'Object' is defined in an assembly that is not referenc ...
- 记一次jdk升级引起的 Unsupported major.minor version 51.0
之前jdk 一直是1.6,tomcat 是6.x 版本,, 现在引入的新的jar, 出现 Caused by: java.lang.UnsupportedClassVersionError: org/ ...
- 未能加载文件或程序集“Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5”或它的某一个依赖项。系统找不到指定的文件。
在创建ASP.NET MVC项目过程中发生了这个异常 未能加载文件或程序集"Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0 ...
- 无法解决“Microsoft.SharePoint.Security, Version=15.0.0.0,”与“Microsoft.SharePoint.Security, Version=14.0.0.0”之间的冲突
VisualStudio 2013创建控制台项目,.NetFramework选为4.5.生成目标平台:x64.然后添加对Microsoft.SharePoint.dll的引用. 生成项目时," ...
随机推荐
- 算法Sedgewick第四版-第1章基础-025-用队列实现unix下的Directory命令
package algorithms.util; /************************************************************************** ...
- 算法Sedgewick第四版-第1章基础-020一按优先级计算表达式的值
/****************************************************************************** * Compilation: javac ...
- tab下拉菜单
这个想法早就有的 (写tab下拉菜单)就觉得自己对js不是很熟 所以一直没有写 花了不少时间 <!DOCTYPE html> <html> <head> < ...
- 重构get请求代码---PartyLocation
将原理在PersonDto中定义的partyLocations,剪切到PartyDto中去. @JsonApiToMany private List<PartyLocationDto> p ...
- Java分层概念(转)
Java分层概念(转) 对于分层的概念,似乎之间简单的三层,多了,就有点难以区分了,所以收藏了这个. ervice是业务层 action层即作为控制器 DAO (Data Access Object) ...
- SQL SERVER 取本月上月日期
select dateadd(dd,-day(dateadd(month,-1,getdate()))+1,dateadd(month,-1,getdate())) /*上个月一号*/ s ...
- 使用 create 命令建立数据库v, 并利用sp_helpdb查看数据库文件
create database v on primary (name = v, filename='D:\v.mdf', size=3mb, maxsize=10mb, filegrowth=2mb ...
- c#委托与事件、消息、WndProc用法(转)
c#委托与事件 心得 c#用委托来实现事件通知机制.委托相当与c++函数指针.整个过程涉及一个呼叫者,一个被呼叫者,还有就是这个委托. - 实现步骤有以下几步: 1. 申明委托, 2.定义呼叫者和调用 ...
- Metasploit 读书笔记-持久控制
Meterpreter的persisitence脚本允许主任meterpreter代理,以确保系统重启之后Meterpreter还能运行。如果是反弹连接方式,可以设置连接攻击机的时间间隔。如果是绑定方 ...
- 洛谷P2235 [HNOI2002]Kathy函数
传送门 题解 // luogu-judger-enable-o2 //minamoto #include<cstdio> #include<cstring> using nam ...