django-filter:/goods/ __init__() 的 TypeError 得到了一个意外的关键字参数“名称”
Posted
技术标签:
【中文标题】django-filter:/goods/ __init__() 的 TypeError 得到了一个意外的关键字参数“名称”【英文标题】:django-filter: TypeError at /goods/ __init__() got an unexpected keyword argument 'name' 【发布时间】:2019-01-21 21:00:31 【问题描述】:我正在学习 Django REST 框架。我想使用 django rest 框架中的 django-filter 来选择我想要的结果。 我写了一个filter.py来获取价格在pricemin和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']
然后我在 view.py 中使用了这个过滤器:
class GoodsListViewSet(mixins.ListModelMixin,viewsets.GenericViewSet):
queryset = Goods.objects.all()
serializer_class = GoodsSerializer
pagination_class = GoodsPagination
filter_backends = (DjangoFilterBackend,)
filter_class = GoodsFilter
当我运行我的项目时,出现这样的错误:
TypeError at /goods/
__init__() got an unexpected keyword argument 'name'
我没有在我的代码中使用参数“名称”,所以我不知道这个错误。 追溯:
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']
追溯:
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'
【问题讨论】:
【参考方案1】:来自Migrating to 2.0 指南,
Filter.name
重命名为Filter.field_name
(#792)过滤器名称已重命名为
field_name
,以消除其 FilterSet 类中过滤器的属性名称与用于过滤目的的field_name
的歧义。
所以,从 django-filter==2.0
开始,使用 field_name
而不是 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']
【讨论】:
如果我没有找到这个答案,Python 的错误消息会让我花费数年时间来解决这个问题。以上是关于django-filter:/goods/ __init__() 的 TypeError 得到了一个意外的关键字参数“名称”的主要内容,如果未能解决你的问题,请参考以下文章
salesforce 零基础学习(六十)Wizard样式创建数据
python测试开发django-169.过滤器django-filter 入门使用