django-tenant-schema为所有租户启用管理模块

时间:2018-10-11 14:05:00

标签: python django

我正在使用多租户架构设置django。我经历了https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html 说明并到达具有初始启动屏幕的位置。 我要实现的是为每个租户启用管理模块。 我在settings.py中有以下内容:

#Application definition
SHARED_APPS = (
    'tenant_schemas',  # mandatory, should always be before any django app
    'customers', # you must list the app where your tenant model resides in

    'django.contrib.contenttypes',

    # everything below here is optional

)

TENANT_APPS = (
    'django.contrib.contenttypes',

    # your tenant-specific apps
    # 'myapp.hotels',
    # 'myapp.houses',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',

)

INSTALLED_APPS = [
    'tenant_schemas',
    'customers',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

TENANT_MODEL = "customers.Client"

MIDDLEWARE = [
    'tenant_schemas.middleware.TenantMiddleware',
    '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 = 'sitemanager.urls'
PUBLIC_SCHEMA_URLCONF = 'sitemanager.urls_public'

尝试打开https://django-tenant-schemas.readthedocs.io/en/latest/install.html时出现错误:

DoesNotExist at /admin/login/
Site matching query does not exist.
Request Method: GET
Request URL:    http://localhost:8000/admin/login/?next=/admin/
Django Version: 2.1.2
Exception Type: DoesNotExist
Exception Value:    
Site matching query does not exist.

我的urls.py:

from django.contrib import admin
from django.urls import path

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

我在配置中缺少什么?

3 个答案:

答案 0 :(得分:1)

添加PUBLIC_SCHEMA_NAME = 'public'可以解决问题。

答案 1 :(得分:0)

我认为django.contrib.sites应该位于SHARED_APPS中。

使用django-tenant-schema +1的荣誉

答案 2 :(得分:0)

然后创建一个超级用户:./manage.py tenant_command createsuperuser 可能会派上用场。