2015-05-26 80 views
3

我想創建一個Django-tenant架構的python Django-mako +項目。我跟着指示完全按照教程這裏:python Django migrate_schemas --shared TypeError:hasattr():屬性名稱必須爲字符串

https://django-tenant-schemas.readthedocs.org/en/latest/install.html#basic-settings

然而,當我運行CMD:蟒蛇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

你使用的是什麼版本的Django?我看到這個在您的教程: '#Django的> = 1.7 蟒蛇manage.py migrate_schemas --shared #Django的<1.7 蟒蛇manage.py sync_schemas --shared'我使用1.8.1 – strongMA

+0

,我有嘗試使用兩者。 –

+0

我並不是可以測試任何解決方案的位置,但是您得到的特定錯誤意味着將'alias'傳遞給'hasattr'函數是字符串以外的內容。如果你可以發佈一個完整的堆棧跟蹤,它會更有幫助。 – strongMA

回答

0

這主要是因爲您開始使用該模型而沒有進行遷移或遷移。這就是爲什麼它顯示

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

所以解決方案:刪除使用該未移植模型或字段的代碼。然後做makemigrations並遷移。而已。它在我的情況下工作

相關問題