Django在heroku上部署路由器的多个数据库

时间:2016-06-23 19:00:04

标签: django database postgresql heroku

我正在尝试使用heroku在我的django项目上配置多个数据库和数据库路由器。

使用一个数据库很容易使用

DATABASES = { 'default': dj_database_url.config() }

在我的本地设置中

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME':  'nameuser3',                  
        'USER': 'postgres',
        'PASSWORD': 'XXXXXXXX',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    },
    'admindb': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME':  'nameadmin4',                  
        'USER': 'postgres',
        'PASSWORD': 'xxxxxx',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }    
}

我还设置了数据库路由器来读写特定数据库。

def db_for_read(self, model, **hints):
    if model._meta.app_label == 'sgdb':
        return 'admindb'
    return 'default'

def db_for_write(self, model, **hints):
    if model._meta.app_label == 'sgdb':
        return 'admindb'
    return 'default'

def allow_relation(self, obj1, obj2, **hints):
    db1 = self.DATABASE_APPS_MAPPING.get(obj1._meta.app_label)
    db2 = self.DATABASE_APPS_MAPPING.get(obj2._meta.app_label)
    if db1 and db2:
        return db1 == db2
    return 'None'

def allow_migrate(self, db, app_label, model_name=None, **hints):
    return True

我用谷歌搜索但无法找到关于如何配置数据库路由器以使用heroku数据库的任何结论。关于如何做到这一点的任何线索?

完整settings.py我上传到heroku -

import dj_database_url
import os

"""
Django settings for example project.

Generated by 'django-admin startproject' using Django 1.9.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""



# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'disqus',
    'django.contrib.sitemaps',
    'easy_maps',
    'hitcount',
    'accounts',
    'localflavor',
    'carts',
    'marketing',
    'crispy_forms',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.facebook'
]


CRISPY_TEMPLATE_PACK = 'bootstrap3'
# Sign IN settings
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = True
# Sign UP settings
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_VERIFICATION = 'optional'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
SOCIALACCOUNT_QUERY_EMAIL = True
SOCIALACCOUNT_PROVIDERS = {
    'facebook': {
        'SCOPE': ['email', 'publish_stream'],
        'METHOD': 'js_sdk'  # instead of 'oauth2'
    }
}

SITE_NAME='example.com'


#Newsletter
NEWSLETTER_CONFIRM_EMAIL = False

HITCOUNT_HITS_PER_IP_LIMIT = 0
HITCOUNT_KEEP_HIT_IN_DATABASE = { 'days': 30 }


DISQUS_WEBSITE_SHORTNAME = 'example'

EMAIL_BACKEND =  'django.core.mail.backends.smtp.EmailBackend' 
DEFAULT_FROM_EMAIL = 'example@gmail.com'
EMAIL_HOST =  'smtp.gmail.com'
EMAIL_HOST_USER = 'example@gmail.com'
EMAIL_HOST_PASSWORD = 'example'
EMAIL_USE_TLS = True 
EMAIL_PORT = 587

SITE_ID = 2

DEBUG = False

if DEBUG: 
    SITE_URL = "http://127.0.0.1:8000"
if not DEBUG :
    SITE_URL = "http://www.example.com"


MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    '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',
]

ROOT_URLCONF = 'example.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'example.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases



# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/1.9/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.9/howto/static-files/

# STATIC_URL = '/static/'
# STATIC_ROOT = os.path.join((BASE_DIR), 'static_cdn')
# MEDIA_ROOT = os.path.join((BASE_DIR), 'media_cdn')
# MEDIA_URL = '/media/'

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join((BASE_DIR), 'static_cdn')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join((BASE_DIR), 'media_cdn')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

# db_from_env = dj_database_url.config(conn_max_age=500)
# db_from_env2 = dj_database_url.config(default ='postgres://address', conn_max_age=500)
# DATABASES['default'].update(db_from_env)
# DATABASES['admin'].update(db_from_env2)

DATABASES = {'default': dj_database_url.config(default='postgres://address') , 
            'admin': dj_database_url.config(default='postgres://urladdress')}

DATABASE_ROUTERS = ['exampleapp.routers.DatabaseAppsRouter',]



SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['*']

1 个答案:

答案 0 :(得分:1)

啊终于找到了它。

首先要做。 heroku pg:info。查看哪些数据库网址链接到您的应用。你应该看到你的应用程序的多个数据库。否则,在heroku上创建一个新数据库并将其与您的应用程序连接。

然后在settings.py中保持它与本地settings.py一样。不要使用dj_database_url。默认为database_url。瞧,heroku全部设置为多个dbs。

phew花了这么多小时才弄明白。感谢

df.where(df.homeworkSubmitted==True).count()