Django: TypeError: XXX() got multiple values for argument ‘chat_id’

huangapple go评论94阅读模式
英文:

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> # &#x1f598; no <b>bind=True</b>
def get_response(
chat_id: int, is_first_message: bool, question: str, user_id: int
) -&gt; str:
# &hellip;
pass</code></pre>

indeed, with bound tasks&nbsp;<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.

huangapple
  • 本文由 发表于 2023年7月3日 21:57:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定