我正在尝试使用django-tenant-schema创建一个python django-mako-plus项目。我完全按照教程中的说明进行操作:
https://django-tenant-schemas.readthedocs.org/en/latest/install.html#basic-settings
但是,当我运行cmd:python manage.py migrate_schemas --shared时,我收到以下错误:
File "C:\Python34\lib\site-packages\django\db\utils.py", line 234, in __getitem__
if hasattr(self._connections, alias):
TypeError: hasattr(): attribute name must be string
有哪些可能的问题?
仅供参考,这是我的设置文件:
"""
Django settings for tents_dmp project.
Generated by 'django-admin startproject' using Django 1.8.1.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'ekr-az&e)g_7&&%um+c%652b72#e035a#_y7rv19jl1qj47t)k'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
SHARED_APPS = (
'tenant_schemas', # mandatory
#'customers', # you must list the app where your tenant model resides in
'homepage',
'django.contrib.contenttypes',
# everything below here is optional
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
#'django.contrib.staticfiles',
#'django_mako_plus.controller',
)
TENANT_APPS = (
# The following Django contrib apps must be in TENANT_APPS
'django.contrib.contenttypes',
# your tenant-specific apps
)
INSTALLED_APPS = list(set(SHARED_APPS + TENANT_APPS))
TENANT_MODEL = "homepage.Client" # app.Model
MIDDLEWARE_CLASSES = (
'tenant_schemas.middleware.TenantMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django_mako_plus.controller.router.RequestInitMiddleware',
)
ROOT_URLCONF = 'tents_dmp.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
#'django.core.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',
],
},
},
]
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
#...
)
WSGI_APPLICATION = 'tents_dmp.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'tenant_schemas.postgresql_backend',
'NAME': 'database2',
'USER': 'postgres',
'PASSWORD': 'XXX',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
DATABASE_ROUTERS = (
'tenant_schemas.routers.TenantSyncRouter',
)
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (
# SECURITY WARNING: this next line must be commented out at deployment
BASE_DIR,
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
DEBUG_PROPAGATE_EXCEPTIONS = DEBUG # never set this True on a live site
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'console':{
'level':'DEBUG',
'class':'logging.StreamHandler',
'formatter': 'simple'
},
},
'loggers': {
'django_mako_plus': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': False,
},
},
}
###############################################################
### Specific settings for the Django-Mako-Plus app
DJANGO_MAKO_PLUS = {
# identifies where the Mako template cache will be stored, relative to each app
'TEMPLATES_CACHE_DIR': 'cached_templates',
# the default app and page to render in Mako when the url is too short
'DEFAULT_PAGE': 'index',
'DEFAULT_APP': 'homepage',
# the default encoding of template files
'DEFAULT_TEMPLATE_ENCODING': 'utf-8',
# these are included in every template by default - if you put your most-used libraries here, you won't have to import them exlicitly in templates
'DEFAULT_TEMPLATE_IMPORTS': [
'import os, os.path, re, json',
],
# see the DMP online tutorial for information about this setting
'URL_START_INDEX': 0,
# whether to send the custom DMP signals -- set to False for a slight speed-up in router processing
# determines whether DMP will send its custom signals during the process
'SIGNALS': True,
# whether to minify using rjsmin, rcssmin during 1) collection of static files, and 2) on the fly as .jsm and .cssm files are rendered
# rjsmin and rcssmin are fast enough that doing it on the fly can be done without slowing requests down
'MINIFY_JS_CSS': True,
# see the DMP online tutorial for information about this setting
'TEMPLATES_DIRS': [
# '/var/somewhere/templates/',
],
}
### End of settings for the Django-Mako-Plus
################################################################
和主页应用中的models.py文件:
from django.db import models
from tenant_schemas.models import TenantMixin
class Client(TenantMixin):
name = models.CharField(max_length=100)
paid_until = models.DateField()
on_trial = models.BooleanField()
created_on = models.DateField(auto_now_add=True)
auto_create_schema = True
非常感谢任何帮助
C:\Users\Desktop\django\tents_dmp>python manage.py migrate_schemas
--shared
=== Running migrate for schema public
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
338, in execute_from_command_line
utility.execute()
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python34\lib\site-packages\tenant_schemas\management\commands\migrate
_schemas.py", line 24, in run_from_argv
super(MigrateSchemasCommand, self).run_from_argv(argv)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 390,
in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 441,
in execute
output = self.handle(*args, **options)
File "C:\Python34\lib\site-packages\tenant_schemas\management\commands\migrate
_schemas.py", line 34, in handle
self.run_migrations(self.schema_name, settings.SHARED_APPS)
File "C:\Python34\lib\site-packages\tenant_schemas\management\commands\migrate
_schemas.py", line 61, in run_migrations
command.execute(*self.args, **defaults)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 441,
in execute
output = self.handle(*args, **options)
File "C:\Python34\lib\site-packages\django\core\management\commands\migrate.py
", line 70, in handle
connection = connections[db]
File "C:\Python34\lib\site-packages\django\db\utils.py", line 234, in __getite
m__
if hasattr(self._connections, alias):
TypeError: hasattr(): attribute name must be string
答案 0 :(得分:0)
这是你的问题:
https://github.com/bernardopires/django-tenant-schemas/issues/252
DTS仍然无法与django 1.8一起使用,但这里有一个包含更改内容的分叉。
https://github.com/tomturner/django-tenant-schemas/commit/fe437f7c81af8484614b3dcdaeb7ad5c43249f54
看起来它适用于1.7和1.8,但我还没有经过测试。
由于我开始了一个新项目,我可能会对django降级一段时间。
答案 1 :(得分:0)
这主要是因为您在不进行makemigrations或迁移的情况下开始使用该模型。这就是为什么它显示
if hasattr(self._connections, alias):
TypeError: hasattr(): attribute name must be string
所以解决方案:删除使用该未迁移模型或字段的代码。然后做makemigrations并迁移。而已。它适用于我的情况