python - Django:使用 Django 租户创建主站点
问题描述
我想让我的博客 django 项目租户知道,我有一个主站点和两个租户。我已经能够创建租户,问题出在主站点上。我希望主站点显示所有租户文章。我该怎么做?
我从settings.py
下面的文件创建了我的主站点,我收到以下错误:
ProgrammingError at /
relation "articles_article" does not exist
LINE 1: ...cle"."thumb", "articles_article"."author_id" FROM "articles_...
设置.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
SHARED_APPS = (
'django_tenants', # mandatory
'customers', # you must list the app where your tenant model resides in
'django.contrib.contenttypes',
# everything below here is optional
# 'django.contrib.auth',
'django.contrib.staticfiles',
'django.contrib.sessions',
# 'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
)
TENANT_APPS = (
'django.contrib.auth',
'django.contrib.staticfiles',
'django.contrib.sessions',
# 'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'django.contrib.contenttypes',
'articles',
'accounts',
)
INSTALLED_APPS = list(SHARED_APPS) + [app for app in TENANT_APPS if app not in SHARED_APPS]
TENANT_MODEL = "customers.Client" # app.Model
TENANT_DOMAIN_MODEL = "customers.Domain" # app.Model
MIDDLEWARE = [
'django_tenants.middleware.main.TenantMainMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'Reader.urls'
PUBLIC_SCHEMA_NAME = 'public'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'Reader.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django_tenants.postgresql_backend',
'NAME': 'tenant_blog',
'USER': 'posts',
'HOST': 'locst',
'PORT': 5432,
'PASSWORD': 'ps',
}
}
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
DEFAULT_FILE_STORAGE = "django_tenants.files.storage.TenantFileSystemStorage"
STATICFILES_STORAGE = "django_tenants.staticfiles.storage.TenantStaticFilesStorage"
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MULTITENANT_RELATIVE_STATIC_ROOT = ""
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MULTITENANT_RELATIVE_MEDIA_ROOT = "" # (default: create sub-directory for each tenant)
DATABASE_ROUTERS = (
'django_tenants.routers.TenantSyncRouter',
)
解决方案
推荐阅读
- amazon-web-services - 您能否通过 AWS 使用 ADFS 单点登录来预先选择账户和/或角色?
- java - Mockito/Spy 不工作,执行原始功能
- javascript - if 语句,第一个条件的值在第二个条件的范围内,但它返回第一个条件
- python - 在 Python 中搜索数组
- node.js - Azure Linux Web 应用程序(节点)崩溃并显示消息:“分段错误(核心转储)”并重新启动
- c# - 适合分布式系统中工作编排/任务负载平衡的库/工具
- model - 原油蒸馏装置 Simulink 模型仿真
- dataframe - 在pyspark中循环数据帧
- linux - IntelliJ 查找 SSH 密码
- python - 我的 tkinter 程序在按下按钮时修改标签出现故障(关闭)