【问题标题】:Impossible to use django-allauth when there is already an `account` app?已经有“帐户”应用程序时无法使用 django-allauth?
【发布时间】:2020-02-12 07:14:02
【问题描述】:

我的 Django 应用程序已经有一个名为 account 的应用程序。这是否意味着由于名称冲突,绝对不可能使用django all-auth?由于现有数据,无法重命名应用account

settings.py:

INSTALLED_APPS = [
    ...
    'account',
    ...

    # For allauth:
    'django.contrib.sites',
    'allauth',
    'allauth.account',    # Name conflict
    ...

如果有,有什么好的选择吗?


  1. 2-14

根据 solarissmoke 的建议。我应该把新应用程序放在哪里,它的名称是什么? 是这样的吗(当然是错的)?

my_project/account/apps.py:

import allauth.account
from django.apps import AppConfig


class AccountConfig(AppConfig):
    name = 'account'


class AllAuthAccountConfig(allauth.account):
    name = 'allauth.account'
    label = 'allauth_account'  # Change this
    verbose_name = 'aullauth_account'

【问题讨论】:

    标签: django django-allauth


    【解决方案1】:

    这是 django-allauth 的 known problem

    您可以通过将自己的应用更改为使用不同的app label 来解决此问题。在您应用的AppConfig

    class AccountConfig(AppConfig):
        name = 'my_project.apps.account'
        label = 'my_project_account'  # Change this
        verbose_name = 'account'
    

    并在您的INSTALLED_APPS 中引用此应用配置,例如,

    INSTALLED_APPS = [
        ...
        'account.apps.AccountConfig',
        ...
    
        'allauth',
        'allauth.account',
        ...
    

    现在应该可以工作了,因为应用标签是独一无二的。请注意,唯一的问题是您的 account 应用程序的数据库表名称必须更改,以免与 allauth 应用程序冲突 - 这将需要一些数据迁移(如果在已建立的项目上)或创建新的迁移(如果在一个您可以负担破坏数据库的项目中)。

    您也可以使用 allauth.account 应用程序执行此操作,如果这更容易的话 - 只需在项目的任何位置创建一个新的应用程序配置,例如,

    my_project/allauth_apps/apps.py(确保还在这个新目录中创建__init__.py):

    class AllAuthAccountConfig(allauth.account):
        name = 'allauth.account'
        label = 'allauth_account'  # Change this
        verbose_name = 'aullauth_account'
    

    然后在您的INSTALLED_APPS 中将account 替换为my_project.allauth_apps.apps.AllAuthAccountConfig。如上所述,这会更改数据库表名。

    【讨论】:

    • 重命名allauth的account怎么样?
    • 您也应该能够做到这一点 - 只需创建一个新的应用配置来继承 allauth.account 应用配置,给它一个新标签,然后用它来代替 account。跨度>
    • 我已经编辑了答案,详细说明了如何对 allauth 应用程序进行子类化。
    • 按照您的建议做了:1) 在my_project 下,添加allauth_apps 目录,其中包含__init__.py 文件。 2)用allauth_apps.apps.AllAuthAccountConfig替换allauth.account(应该删除my_project,对吧?)。 3) 在allauth_apps 目录中添加一个apps.py 文件,其中包含您描述的内容。但是,现在我收到了class AllAuthAccountConfig(allauth.account): 行的module.__init__() takes at most 2 arguments (3 given) 错误消息(在那个文件中,我应该`import allauth.account,对吗?)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多