Skip to main content

Use connection URLs for Database/Cache settings in your Django project.

Project description

https://secure.travis-ci.org/f0r4y312/django-connection-url.png?branch=master

This is a fork of Kenneth Reitz’s DJ-Database-URL extended to support cache URLs as well.

There are some significant changes to the original, most notably, .parse() has been dropped. You can pass URLs and environment variables to .config(). The passed value will be first checked if it is an environment variable, and if not found it will be parsed as a URL. .config() will not use DATABASE_URL as default and therefore cannot be called without parameters.

The connection_url.config method returns a Django database/cache connection dictionary, populated with all the data specified in your URL. Multiple dictionary objects can be passed to .config() to set default values for settings and options. Keyword arguments passed will override any default or parsed values.

This simple Django utility allows you to utilize the 12factor inspired DATABASE_URL environment variable to configure your Django application.

Supported Databases

Support currently exists for PostgreSQL, PostGIS, MySQL, MySQL (GIS), Oracle, Oracle (GIS), and SQLite.

Supported Caches

Support currently exists for Redis, Memcached and local memory cache.

Installation

Installation is simple:

$ pip install django-connection-url

Usage

Configure your database in settings.py from DATABASE_URL:

import connection_url

DATABASES['default'] = connection_url.config('DATABASE_URL')

Provide an arbitrary Database URL:

DATABASES['default'] = connection_url.config('postgres://...')

Set default values and override with keyword arguments:

connection_url.config('mysql://username:password@hostname:5432/database?TIMEOUT=100', {
                      'ENGINE': 'django.contrib.gis.db.backends.postgis',
                      'CONN_MAX_AGE': 1000,
                      'OPTIONS': {'MAX_CONNECTIONS': 120},
                      }, ENGINE='django.db.backends.postgresql_psycopg2')
#produces the following result
{
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'database',
    'HOST': 'hostname',
    'PORT': 5432,
    'USER': 'username',
    'PASSWORD': 'password',
    'CONN_MAX_AGE': 1000,
    'OPTIONS': {'TIMEOUT': '100','MAX_CONNECTIONS': 120},
}

The order of precedence for dict arguments, parsed values and keyword arg overrides is: Keyword arguments > Parsed values from connection URL > Defaults from passed dict objects.

URL schema

Engine

Django Backend

URL

PostgreSQL

django.db.backends.postgresql_psycopg2

postgres://USER:PASSWORD@HOST:PORT/NAME [1]

PostGIS

django.contrib.gis.db.backends.postgis

postgis://USER:PASSWORD@HOST:PORT/NAME

MySQL

django.db.backends.mysql

mysql://USER:PASSWORD@HOST:PORT/NAME

MySQL (GIS)

django.contrib.gis.db.backends.mysql

mysqlgis://USER:PASSWORD@HOST:PORT/NAME

SQLite

django.db.backends.sqlite3

sqlite:///PATH [2]

Oracle

django.db.backends.oracle

oracle://USER:PASSWORD@HOST:PORT/NAME [3]

Oracle (GIS)

django.contrib.gis.db.backends.oracle

oraclegis://USER:PASSWORD@HOST:PORT/NAME

Redis

redis_cache.RedisCache

redis://USER:PASSWORD@HOST:PORT [4]

Memcached

django.core.cache.backends.memcached.MemcachedCache

memcached://USER:PASSWORD@HOST:PORT

Local memory

django.core.cache.backends.locmem.LocMemCache

locmem://HOST:PORT/PATH [5]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-connection-url-0.1.2.tar.gz (5.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page