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的引用. 生成项目时," ...
随机推荐
- C++ 结构体的构造函数和析构函数
在C++中除了类中可以有构造函数和析构函数外,结构体中也可以包含构造函数和析构函数,这是因为结构体和类基本雷同,唯一区别是,类中成员变量默认为私有,而结构体中则为公有.注意,C++中的结构体是可以有析 ...
- JavaPersistenceWithMyBatis3笔记-第4章SQL Mappers Using Annotations-001
一. 1.Mapper /** * */ package com.mybatis3.mappers; import org.apache.ibatis.annotations.Select; impo ...
- Person.delete请求----强大的bug---下班之前总结整个过程
默认访问的是: findAll(query) 还有个findAll(ids,query) 只有findAll才调用了findEntity->findById: 那么我重写了findById,查询 ...
- java全栈day09----继承 抽象类
01继承的概述 在Java中,类的继承是指在一个现有类的基础上去构建一个新的类, 构建出来的新类被称作子类,现有类被称作父类在java中 继承如何来实用呢?举个例子 继承的定义格式和使用 *A:继承的 ...
- Bootstrap 的 Dropdown
一.简介 Dropdown 就是下拉列表,这里 有一个例子. Dropdown 的完整代码如下: <div id="dropdownWrapper"> <butt ...
- 【Arcgis for android】程序运行出错原因分析及解决(超详细)
查看项目下是否有libs文件夹,正常情况下其中应该有 如果没有,在项目上右键 ->arcgis tools->convert to arcgis android project 排除了上述 ...
- Arcgis android 10.2安装方法
请仔细对照博文做!!! 将arcgis android 10.2的压缩包解压 arcgis android 10.2下载地址http://pan.baidu.com/s/1sj2LKO9 Help-& ...
- Net.Core导入EXCel文件里的数据
1.前台的表单: <form enctype="multipart/form-data" method="post" id="inportFil ...
- GridView控件点击单元格如何获取该列的列标题
本博文Insus.NET教你实现在GridView控件中,用mouse点击某单元格之后,希望能获取到该列的列标题. 创建一个网页,创建一个GridView控件: 去cs绑定数据给GridView控件: ...
- java泛型与object的比较
在Java SE 1.5之前,没有泛型的情况的下,通过对类型Object的引用来实现参数的“任意化”,“任意化”带来的缺点是要做显式的强制类型转换,而这种转换是要求开发者对实际参数类型可以预知的情况下 ...