【问题标题】:Getting error search_products() takes at least 2 arguments (2 given)获取错误 search_products() 至少需要 2 个参数(给定 2 个)
【发布时间】:2012-10-07 00:02:00
【问题描述】:

由于某种原因,我收到了错误:

TypeError: search_products() takes at least 2 arguments (2 given)

奇怪的是,我在两个不同的地方进行了相同的 API 调用——一个在我放置在 Model 类之一中的函数中。页面视图中的另一个。模型类中的那个可以正常工作,而 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)

【问题讨论】:

    标签: python django function default required-field


    【解决方案1】:

    search_products 的定义中,您将category_id 作为必填字段,并且在调用该方法时没有将其作为参数提供。为category_id 提供默认值或传递适当的参数来解决您的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 2014-09-20
      • 2019-12-12
      • 2011-12-25
      • 1970-01-01
      相关资源
      最近更新 更多