【Django Rest Framework】TypeError: __init__() got an unexpected keyword argument 'name'
前端Vue问题描述:
通过面包屑导航,无法获取导航分类对应商品列表
后端问题描述:
# 报错提示
File "D:\Envs\vueshop\lib\site-packages\django_filters\filterset.py", line 258, in form Form = self.get_form_class() File "D:\Envs\vueshop\lib\site-packages\django_filters\filterset.py", line 250, in get_form_class for name, filter_ in self.filters.items()]) File "D:\Envs\vueshop\lib\site-packages\django_filters\filterset.py", line 250, in <listcomp> for name, filter_ in self.filters.items()]) File "D:\Envs\vueshop\lib\site-packages\django_filters\filters.py", line 140, in field self._field = self.field_class(label=self.label, **field_kwargs) File "D:\Envs\vueshop\lib\site-packages\django\forms\fields.py", line 329, in __init__ super().__init__(max_value=max_value, min_value=min_value, **kwargs) File "D:\Envs\vueshop\lib\site-packages\django\forms\fields.py", line 253, in __init__ super().__init__(**kwargs) TypeError: __init__() got an unexpected keyword argument 'name'
因为报错内容中多次提到了django_filters,所以主动去filter文件中寻找name
pricemin = django_filters.NumberFilter(name='shop_price', help_text="最低价格", lookup_expr='gte')
后经百度查找得知,Django2版本之后,这里用field_name 代替了 name
pricemin = django_filters.NumberFilter(field_name ='shop_price', help_text="最低价格", lookup_expr='gte')
至此问题解决!
人生短短数十载,经不起几次重头再来