gpt4 book ai didi

python - 如何使用 Django 正确设置自定义用户模型和多个数据库?

转载 作者:行者123 更新时间:2023-12-05 04:23:38 25 4
gpt4 key购买 nike

我创建了一个自定义应用程序来对用户进行身份验证,并且很难让所有内容都适用于多个数据库。

基本上,我创建了多个 Django 项目并想要一个用于身份验证的唯一数据库和一个自定义 AUTH_USER_MODEL。数据库在 settings.py 中声明有两个路由器,一个用于 authcontenttypesauthentication 应用程序,另一个路由器用于其余的应用程序。

我成功迁移了自定义 User 模型并使用选项 --database=auth_db 创建了 createsuperuser 但是,当我在管理部分在我的应用程序之一中创建对象 Django 抛出错误:

IntegrityError at /admin/django_celery_beat/crontabschedule/add/
insert or update on table "django_admin_log" violates foreign key constraint "django_admin_log_user_id_c564eba6_fk_authentication_user_id"
DETAIL: Key (user_id)=(1) is not present in table "authentication_user".

如您所见,它表示 ID 为 1 的用户未创建,但我使用它登录并且我 100% 确定自定义 User 模型是在 身份验证应用程序:

root@ec00652b9b9a:/app# python manage.py migrate authentication --database=auth_db
Operations to perform:
Apply all migrations: authentication
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying authentication.0001_initial... OK

root@ec00652b9b9a:/app# python manage.py migrate contenttypes --database=auth_db
Operations to perform:
Apply all migrations: contenttypes
Running migrations:
No migrations to apply.

root@ec00652b9b9a:/app# python manage.py migrate auth --database=auth_db
Operations to perform:
Apply all migrations: auth
Running migrations:
No migrations to apply.

root@ec00652b9b9a:/app# python manage.py migrate admin --database=default
Operations to perform:
Apply all migrations: admin
Running migrations:
Applying admin.0001_initial... OK

root@ec00652b9b9a:/app# python manage.py migrate sessions --database=default
Operations to perform:
Apply all migrations: sessions
Running migrations:
Applying sessions.0001_initial... OK

这是否意味着 Celery beatadmin 正在查找错误的数据库?我怎样才能进一步调查这个问题?

databases_router.py

class AuthRouter:
route_app_labels = {'auth', 'contenttypes', 'authentication'}

def db_for_write(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'auth_db'
return None

def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'auth_db'
return None

def allow_relation(self, obj1, obj2, **hints):
if (
obj1._meta.app_label in self.route_app_labels or
obj2._meta.app_label in self.route_app_labels
):
return True
return None

def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in self.route_app_labels:
return db == 'auth_db'
return None


class AppsRouter:

def db_for_write(self, model, **hints):
return 'default'

def db_for_read(self, model, **hints):
return 'default'

def allow_relation(self, obj1, obj2, **hints):
return True

def allow_migrate(self, db, app_label, model_name=None, **hints):
return True

settings.py

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': env("PROJECT_DB_NAME"),
'HOST': env("PROJECT_DB_HOST"),
'PORT': env("PROJECT_DB_PORT"),
'USER': env("PROJECT_DB_USER"),
'PASSWORD': env("PROJECT_DB_PASSWORD"),
'OPTIONS': {'sslmode': 'require',
'options': '-c search_path=public'
},
},
'auth_db': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': env("AUTHENTICATION_DB_NAME"),
'HOST': env("AUTHENTICATION_DB_HOST"),
'USER': env("AUTHENTICATION_DB_USER"),
'PORT': env("AUTHENTICATION_DB_PORT"),
'PASSWORD': env("AUTHENTICATION_DB_PASSWORD"),
'OPTIONS': {'sslmode': 'require',
'options': '-c search_path=public'
},
},
}

DATABASE_ROUTERS = ['project.databases_router.AuthRouter', 'project.databases_router.AppsRouter']

AUTH_USER_MODEL = 'authentication.User'

最佳答案

我不确定最近的 Django 版本,但我不记得 Django(或 Postgres)能够处理与其他数据库的外键关系(Django 的 LogEntry 模型依赖于一个)

省略 Django 管理并自己编写一些东西或修补日志记录模型可能更容易。

但是,我会重新评估您将用户与默认数据库完全分开的要求。如果您真的要将用户与上下文应用程序分开,您可能也不应该依赖任何外键来访问用户相关数据。

关于python - 如何使用 Django 正确设置自定义用户模型和多个数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73688237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com