Python Django migrate_schemas --shared TypeError: hasattr (): attribute name must be string on

I am trying to create a django-mako-plus python project with django-tenant-schema. I followed the instructions exactly like the tutorial here:

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

However, when I run cmd: python manage.py migrate_schemas --shared, I get the following error:

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

      

What are the possible problems?

FYI, here is my settings file:

"""
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
 ################################################################

      

and my models.py file in the home page app:

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

      

Any help would be greatly appreciated

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

      

+3


source to share


2 answers


Here is your problem:

https://github.com/bernardopires/django-tenant-schemas/issues/252

DTS still doesn't work with django 1.8, but here is the fork that has the material changed.



https://github.com/tomturner/django-tenant-schemas/commit/fe437f7c81af8484614b3dcdaeb7ad5c43249f54

It looks like it works with 1.7 and 1.8, but I haven't tested.

As I am starting a new project, I might downgrade django a bit.

0


source


This is mainly because you started using the model without mocking or migration. This is why it shows

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

      



so the solution is: remove the code that uses this non-graphical model or fields. Then mock up and migrate. This is it. It worked in my case

0


source







All Articles