英文:
Django: TypeError: XXX() got multiple values for argument 'chat_id'
问题
我正在使用Django和Celery,并且遇到了这个错误:
TypeError: get_response()的参数'chat_id'收到了多个值
我通过以下方式调用该函数:
i = get_response.delay(chat_id=chat_id, is_first_message=is_first_message, question=question, user_id=user_id)
函数的头部如下:
@shared_task(bind=True)
def get_response(chat_id: int, is_first_message: bool, question: str, user_id: int) -> str:
我尝试运行了print(chat_id)
,但这只确认了该字段是一个单一的整数。
编辑。完整的跟踪信息:
[03/Jul/2023 13:52:16] ERROR "django.request" Internal Server Error: /chat/chat/1/283/new_message/
project | Traceback (most recent call last):
project | File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
project | response = get_response(request)
project | File "/usr/local/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
project | response = wrapped_callback(request, *callback_args, **callback_kwargs)
project | File "/code/apps/chat/views.py", line 82, in new_message
project | i = get_response.delay(chat_id=chat_id, is_first_message=is_first_message, question=question, user_id=user_id)
project | File "/usr/local/lib/python3.11/site-packages/celery/app/task.py", line 425, in delay
project | return self.apply_async(args, kwargs)
project | File "/usr/local/lib/python3.11/site-packages/celery/app/task.py", line 540, in apply_async
project | check_arguments(*(args or ()), **(kwargs or {}))
project | TypeError: get_response()的参数'chat_id'收到了多个值
project | [03/Jul/2023 13:52:16] ERROR "django.server" "POST /chat/chat/1/283/new_message/ HTTP/1.1" 500 87645
英文:
I'm using Django with Celery and getting this error:
TypeError: get_response() got multiple values for argument 'chat_id'
I'm calling the function via:
i = get_response.delay(chat_id=chat_id, is_first_message=is_first_message,question=question,user_id=user_id)
and the function header is:
@shared_task(bind=True)
def get_response(chat_id: int, is_first_message: bool,
question:str,user_id:int) -> str:
I tried running a print(chat_id) but this only confirmed the field was a single integer.
Edit. Full trace:
[03/Jul/2023 13:52:16] ERROR "django.request" Internal Server Error: /chat/chat/1/283/new_message/
project | Traceback (most recent call last):
project | File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
project | response = get_response(request)
project | ^^^^^^^^^^^^^^^^^^^^^
project | File "/usr/local/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
project | response = wrapped_callback(request, *callback_args, **callback_kwargs)
project | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
project | File "/code/apps/chat/views.py", line 82, in new_message
project | i = get_response.delay(chat_id=chat_id, is_first_message=is_first_message,question=question,user_id=user_id)
project | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
project | File "/usr/local/lib/python3.11/site-packages/celery/app/task.py", line 425, in delay
project | return self.apply_async(args, kwargs)
project | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
project | File "/usr/local/lib/python3.11/site-packages/celery/app/task.py", line 540, in apply_async
project | check_arguments(*(args or ()), **(kwargs or {}))
project | TypeError: get_response() got multiple values for argument 'chat_id'
project | [03/Jul/2023 13:52:16] ERROR "django.server" "POST /chat/chat/1/283/new_message/ HTTP/1.1" 500 87645
答案1
得分: 2
应该将bind=True
移除,的确:
@shared_task # 🖘 no bind=True
def get_response(
chat_id: int, is_first_message: bool, question: str, user_id: int
) -> str:
# ...
pass
确实,对于bound tasks [celery-doc],任务实例作为第一个参数传递,因此你应该要么将self
(或task
或其他一些参数)添加为第一个参数,要么移除bind=True
,因为你似乎在函数中并未使用它。
英文:
You should leave the bind=True
out, indeed:
<pre><code><b>@shared_task</b> # 🖘 no <b>bind=True</b>
def get_response(
chat_id: int, is_first_message: bool, question: str, user_id: int
) -> str:
# …
pass</code></pre>
indeed, with bound tasks <sup>[celery-doc]</sup> the task instance is passed as first parameter, so you should either add self
(or task
or some other parameter) as first parameter, or remove the bind=True
, since you don't seem to use it in your function anyway.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论