Python Django migrate_模式--共享类型错误:hasattr():属性名称必须为字符串

Python Django migrate_模式--共享类型错误:hasattr():属性名称必须为字符串,python,django,postgresql,mako,dmp,Python,Django,Postgresql,Mako,Dmp,我正在尝试创建一个带有django租户模式的PythonDjango mako plus项目。我完全按照教程中的说明操作: 但是,当我运行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): TypeE

我正在尝试创建一个带有django租户模式的PythonDjango mako plus项目。我完全按照教程中的说明操作:

但是,当我运行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
 ################################################################
主页应用程序中的my 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
C:\Users\Desktop\django\tents\u dmp>python manage.py migrate\u模式
--共享
==正在为公共架构运行迁移
回溯(最近一次呼叫最后一次):
文件“manage.py”,第10行,在
从命令行(sys.argv)执行命令
文件“C:\Python34\lib\site packages\django\core\management\\uuuu init\uuuuu.py”,第行
338,在从命令行执行命令
utility.execute()
文件“C:\Python34\lib\site packages\django\core\management\\uuuu init\uuuuu.py”,第行
330,执行中
self.fetch_命令(子命令)。从_argv(self.argv)运行_
文件“C:\Python34\lib\site packages\tenant\u schemas\management\commands\migrate
_schemas.py”,第24行,从_argv运行_
超级(MigrateSchemasCommand,self)。从_argv(argv)运行_
文件“C:\Python34\lib\site packages\django\core\management\base.py”,第390行,
从_argv运行_
self.execute(*args,**cmd_选项)
文件“C:\Python34\lib\site packages\django\core\management\base.py”,第441行,
执行中
输出=self.handle(*args,**选项)
文件“C:\Python34\lib\site packages\tenant\u schemas\management\commands\migrate
_schemas.py”,第34行,在句柄中
self.run_迁移(self.schema_名称、设置、共享_应用)
文件“C:\Python34\lib\site packages\tenant\u schemas\management\commands\migrate
_schemas.py”,第61行,运行迁移中
command.execute(*self.args,**默认值)
文件“C:\Python34\lib\site packages\django\core\management\base.py”,第441行,
执行中
输出=self.handle(*args,**选项)
文件“C:\Python34\lib\site packages\django\core\management\commands\migrate.py
“,第70行,在手柄中
连接=连接[db]
文件“C:\Python34\lib\site packages\django\db\utils.py”,第234行,在
m__
如果hasattr(self.\u连接,别名):
TypeError:hasattr():属性名称必须是字符串
这是您的问题:

DTS仍然不能与django 1.8一起使用,但是这里有一个fork,它改变了内容

看起来它适用于1.7和1.8,但我还没有测试


由于我正在启动一个新项目,我可能会暂时降级django。

这主要是因为您开始使用模型时没有执行makemigrations或migrate。这就是为什么它会出现

    if hasattr(self._connections, alias):
    TypeError: hasattr(): attribute name must be string

因此,解决方案是:删除使用未分割模型或字段的代码。然后进行makemigrations和migrate。就这样。它在我的情况下起作用了

您使用的是什么版本的django?我在您的教程中看到了这一点:
#Django>=1.7 python manage.py migrate_schemas--shared#Django<1.7 python manage.py sync_schemas--shared
我正在使用1.8.1,并且我已经尝试过使用这两种模式。我无法测试任何解决方案,但是您得到的特定错误意味着传递给
hasattr
函数的
alias
不是字符串。如果你能发布一个完整的堆栈跟踪,那会更有帮助。我在问题的末尾添加了跟踪-