回到顶部

CONTENTS

django_filters TypeError: __init__() got an unexpected keyword argument 'name'

# 错误信息:

  File "E:\py_virtualenv\tracbug\lib\site-packages\django_filters\rest_framework\filterset.py", line 24, in form
    form = super().form
  File "E:\py_virtualenv\tracbug\lib\site-packages\django_filters\filterset.py", line 258, in form
    Form = self.get_form_class()
  File "E:\py_virtualenv\tracbug\lib\site-packages\django_filters\filterset.py", line 250, in get_form_class
    for name, filter_ in self.filters.items()])
  File "E:\py_virtualenv\tracbug\lib\site-packages\django_filters\filterset.py", line 250, in <listcomp>
    for name, filter_ in self.filters.items()])
  File "E:\py_virtualenv\tracbug\lib\site-packages\django_filters\filters.py", line 140, in field
    self._field = self.field_class(label=self.label, **field_kwargs)
  File "E:\py_virtualenv\tracbug\lib\site-packages\django\forms\fields.py", line 228, in __init__
    super(CharField, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'name'

# 解决方法

django-filter 参数名字已经由 name 更改为 field_name

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
@author: yinzhuoqun
@site: http://zhuoqun.info/
@email: yin@zhuoqun.info
@time: 2019/10/18 18:54
"""

from .models import BugInfo
import django_filters



class BugsFilter(django_filters.rest_framework.FilterSet):
    """
    bug 的过滤类
    """
    # 老版本写法:name='xxx', 新版本要用 field_name='xxx'
    # views_max = django_filters.NumberFilter(name='num_views', lookup_expr='lte', help_text="最高浏览量过滤",
    #                                         label="浏览数量 小于等于此设置值")
    title = django_filters.CharFilter(field_name='title', lookup_expr='icontains', help_text="标题过滤",
                                      label="标题 不区分大小写过滤")

    class Meta:
        model = BugInfo
        fields = ["title", ]

 

^_^
请喝咖啡 ×

前一篇: django REST framework 3.10.3 访问 /api/docs/ 报错 'AutoSchema' object has no attribute 'get_link'
下一篇: 台式机装机心得