TypeError: __init__() got an unexpected keyword argument 'providing_args'

TypeError: __init__() got an unexpected keyword argument 'providing_args'

我正在创建 Django 网站。我最近使用 allauth 包添加了 permissions/search 功能。当我尝试通过 docker 运行 网站时,我收到错误消息:

文件“/usr/local/lib/python3.9/site-packages/allauth/account/signals.py”,第 5 行,在 user_logged_in = 信号(providing_args=["请求", "用户"]) TypeError: init() 得到了一个意外的关键字参数 'providing_args'

导致此错误的原因是什么?我知道类型错误通常是由不正确的 models.py 文件引起的,但我似乎无法访问该文件,因为它是外部包的一部分。

Urls.py

urlpatterns = [
    path('admin/', admin.site.urls),

    path('accounts/', include('allauth.urls')),

    path('accounts/', include('accounts.urls')),
    
    path('', include('climate.urls')),
    
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

if settings.DEBUG:
    import debug_toolbar
    urlpatterns = [
        path('__debug__/', include(debug_toolbar.urls)),
    ] + urlpatterns

Models.py

class Country(models.Model):
    id = models.UUIDField(
        primary_key= True,
        db_index = True,
        default=uuid.uuid4,
        editable= False
    )
    name = models.CharField(max_length=50)
    population = models.IntegerField(default=1)
    emissions = models.FloatField(default=1)
    reason = models.CharField(default="", max_length=100)
    flags = models.ImageField(upload_to='images/', default="")
    page = models.URLField(max_length=300, default="")

    def save(self, *args, **kwargs):
        super(Country, self).save(*args, **kwargs)

    class Meta:
        verbose_name_plural = 'countries'
        indexes = [
            models.Index(fields=['id'], name='id_index')
        ]
        permissions = {
            ("special_status", "Can read all countries")
        }

    def __str__(self):
        return self.name

    def flag(self):
        return u'<img src="%s" />' % (self.flags.url)

    def get_absolute_url(self):
        return reverse('country_detail', args =[str(self.id)])

    flag.short_description = 'Flag'

我的 settings.py 处理 allauth。

AUTH_USER_MODEL = 'accounts.CustomUser'
LOGIN_REDIRECT_URL = 'climate:home'
ACCOUNT_LOGOUT_REDIRECT = 'climate:home'
ACCOUNT_SESSION_REMEMBER = True
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = False
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True

完整追溯:

Traceback (most recent call last):

  File "/usr/local/lib/python3.9/threading.py", line 954, in _bootstrap_inner

    self.run()

  File "/usr/local/lib/python3.9/threading.py", line 892, in run

    self._target(*self._args, **self._kwargs)

  File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper

    fn(*args, **kwargs)

  File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 115, in inner_run

    autoreload.raise_last_exception()

  File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception

    raise _exception[1]

  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 381, in execute

    autoreload.check_errors(django.setup)()

  File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper

    fn(*args, **kwargs)

  File "/usr/local/lib/python3.9/site-packages/django/__init__.py", line 24, in setup

    apps.populate(settings.INSTALLED_APPS)

  File "/usr/local/lib/python3.9/site-packages/django/apps/registry.py", line 114, in populate

    app_config.import_models()

  File "/usr/local/lib/python3.9/site-packages/django/apps/config.py", line 300, in import_models

    self.models_module = import_module(models_module_name)

  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import

  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load

  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked

  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked

  File "<frozen importlib._bootstrap_external>", line 790, in exec_module

  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed

  File "/usr/local/lib/python3.9/site-packages/allauth/account/models.py", line 12, in <module>

    from . import app_settings, signals

  File "/usr/local/lib/python3.9/site-packages/allauth/account/signals.py", line 5, in <module>

    user_logged_in = Signal(providing_args=["request", "user"])

TypeError: __init__() got an unexpected keyword argument 'providing_args'

根据评论,您是 运行 Django 4.0 和旧版本的 AllAuth。所以你只需要更新 AllAuth 就可以了。


但是,其他 升级了 AllAuth 的人是 运行 Django 4.0 仍然 看到此错误可能已注册自定义 AllAuth 或其他包含 providing_args 参数的信号。

在这种情况下,您需要在项目中搜索任何信号(例如 AllAuth 中经常被覆盖的信号:user_logged_inemail_changed)并删除 providing_args=['request', 'user', 'signup'] 或其他变体来自 Signal 括号。

有关详细信息,请参阅下文,以及显示如何将每个 providing_args 参数移动到注释行的差异示例。


Django 弃用了在 Django 3.1 中使用 providing_args 参数到 django.dispatch.Signal 的功能。请参阅 3.1 发行说明 Misc section 中的项目符号。

它被删除是因为这个参数除了作为文档之外没有做任何事情。如果这看起来很奇怪,那是因为它很奇怪。参数应指示数据已通过。这可能就是它被弃用的原因。

Django 4.0 继续并完全删除了它,使得任何使用 providing_args 参数调用 Signal() 的代码现在都会触发您遇到的 TypeError:

TypeError: Signal.__init__() got an unexpected keyword argument 'providing_args'

AllAuth 于 2020 年 9 月删除了此参数的使用。(参见 original report on this issue here and the referenced diff

在此线程中提问的人是 运行 AllAuth 0.42.0,它不包括此更改,因此与 Django 4.0 不兼容。

截至今天,Django AllAuth 0.42.0 兼容的最后一个版本是 Django 3.2.9。

我不知道为什么会这样,但是(它在我的机器上工作)我通过替换解决了同样的问题

user_logged_in = Signal(providing_args=["request", "user"])

user_logged_in = Signal()

在/usr/local/lib/python3.9/site-packages/allauth/account/signals.py

这适用于在 Django 4.0 上遇到相同问题但未使用 AllAuth 的任何人。

我运行遇到了类似的问题。但是,我没有使用 AllAuth。 问题是 Django 4.0 没有任何 providing_args 参数,所以信号是这样声明的。

from django.dispatch import Signal

model_delete_signal = Signal()

现在,您可以在发送信号时发送任何参数,这些参数将在接收方 kwargs 中接收。

例如,我在自定义删除函数中发送信号时发送了以下 instance 参数

model_delete_signal.send(sender='session_delete', instance=self)

并在接收器中以这种方式接收

@receiver(session_delete)
def delete_session(sender, **kwargs):
    instance = kwargs['instance']

请注意,对于 Singal()

中没有 providing_args 参数的任何 Django 版本,解决方案都是相同的

我通过替换解决了同样的问题

check_request_enabled = Signal(providing_args=["request"])

check_request_enabled = Signal("request")

现在一切正常。

来自django documentation for django 3.1

The purely documentational providing_args argument for Signal is deprecated. If you rely on this argument as documentation, you can move the text to a code comment or docstring.

你可以在新版本的 Django 4 中使用不带参数的 Signal。像这样

from django.dispatch import Signal, receiver
notification=Signal()
@receiver(notification)
def show_notification(sender, **kwargs):
    print("sender,", sender)
    print("Kwargs", kwargs)
    print("Notification")        

然后在你使用这个信号的地方写成这样

notification.send(sender=None, request=request, user=['Sakib', 'Malik'])

这个问题有更好的解释,但这对我有用...

根据 Django

The purely documentational providing_args argument for Signal is deprecated. If you rely on this argument as documentation, you can move the text to a code comment or docstring.

只需从本地项目存储中的 signals.py 文件中注释掉以下代码:

user_logged_in = Signal(providing_args=["request", "user"])

# Typically followed by `user_logged_in` (unless, e-mail verification kicks in)
user_signed_up = Signal(providing_args=["request", "user"])

password_set = Signal(providing_args=["request", "user"])
password_changed = Signal(providing_args=["request", "user"])
password_reset = Signal(providing_args=["request", "user"])

email_confirmed = Signal(providing_args=["request", "email_address"])
email_confirmation_sent = Signal(
    providing_args=["request", "confirmation", "signup"])

email_changed = Signal(
    providing_args=[
        "request", "user",
        "from_email_address", "to_email_address"])
email_added = Signal(providing_args=["request", "user", "email_address"])
email_removed = Signal(providing_args=["request", "user", "email_address"])

您可以在 /usr/local/lib/python3.10/site-packages/allauth/account/signals.py

中找到它