django-filter:/ goods / __init __()处的TypeError得到了意外的关键字参数'name'

时间:2018-08-14 23:25:28

标签: django django-rest-framework django-filter

我正在学习Django REST Framework。我想在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'

我在代码中没有使用参数'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 个答案:

答案 0 :(得分:6)

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']