出于某种原因,我收到了错误:
TypeError: search_products() takes at least 2 arguments (2 given)
奇怪的是,我在两个不同的地方进行了相同的 API 调用——一个在我放置在其中一个模型类中的函数中。另一个在页面 View 中。模型类中的那个工作正常,而 View 函数中的那个则返回错误。
以下是我在Views.py中的代码:
searchproducts=api.API().search_products(query="myproduct")
同样,当我在 Models.py 中编写完全相同的代码时,一切正常。
我在api.py中的API类中的search_products函数如下:
def search_products(self, category_id, query="", start=0, limit=10, filter=None, ranged_filters=None, sort_by=None):
我如何才能更深入地挖掘以找出发生这种情况的根源?
回溯:
/Users/me/Desktop/myenv2/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
# Apply view middleware
for middleware_method in self._view_middleware:
response = middleware_method(request, callback, callback_args, callback_kwargs)
if response:
return response
try:
response = callback(request, *callback_args, **callback_kwargs) ...
except Exception, e:
# If the view raised an exception, run it through exception
# middleware, and if the exception middleware returns a
# response, use that. Otherwise, reraise the exception.
for middleware_method in self._exception_middleware:
response = middleware_method(request, e)
最佳答案
在 search_products
的定义中,您将 category_id
作为必填字段,并且在调用该方法时没有将其作为参数提供。为 category_id
提供默认值或传入适当的参数以解决您的问题
关于python - 获取错误 search_products() 至少需要 2 个参数(给定 2 个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12764953/