### postgresql_psycopg2_cascade/creation.py # vim: tabstop=4 expandtab autoindent shiftwidth=4 fileencoding=utf-8 from django.db.backends.postgresql_psycopg2.base import DatabaseCreation as ODatabaseCreation # Overload me ## ON DELETE CASCADE ON UPDATE CASCADE class DatabaseCreation(ODatabaseCreation): def sql_for_inline_foreign_key_references(self, field, known_models, style): res = ODatabaseCreation.sql_for_inline_foreign_key_references(self, field, known_models, style) # If not pending if not res[1]: for i in xrange(len(res[0])): res[0][i] = res[0][i].replace('DEFERRABLE', 'ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE') return res def sql_for_pending_references(self, model, style, pending_references): res = ODatabaseCreation.sql_for_pending_references(self, model, style, pending_references) for i in xrange(len(res)): res[i] = res[i].replace('DEFERRABLE', 'ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE') return res def sql_for_many_to_many_field(self, model, f, style): res = ODatabaseCreation.sql_for_many_to_many_field(self, model, f, style) for i in xrange(len(res)): res[i] = res[i].replace('DEFERRABLE', 'ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE') return res # EOF ### postgresql_psycopg2_cascade/base.py # vim: tabstop=4 expandtab autoindent shiftwidth=4 fileencoding=utf-8 from django.db.backends import BaseDatabaseValidation from django.utils.safestring import SafeUnicode, SafeString ## Stuff required for postgresql_psycopg2 imported from django.db.backends.postgresql_psycopg2.base import PostgresqlDatabaseOperations from django.db.backends.postgresql_psycopg2.base import DatabaseClient from django.db.backends.postgresql_psycopg2.base import get_version from django.db.backends.postgresql_psycopg2.base import DatabaseFeatures from django.db.backends.postgresql_psycopg2.base import DatabaseOperations from django.db.backends.postgresql_psycopg2.base import DatabaseWrapper as ODatabaseWrapper # Overload me from django.db.backends.postgresql_psycopg2.base import DatabaseIntrospection ## Our stuff from postgresql_psycopg2_cascade.creation import DatabaseCreation class DatabaseWrapper(ODatabaseWrapper): def __init__(self, *args, **kwargs): super(DatabaseWrapper, self).__init__(*args, **kwargs) self.features = DatabaseFeatures() self.ops = DatabaseOperations() self.client = DatabaseClient() self.creation = DatabaseCreation(self) self.introspection = DatabaseIntrospection(self) self.validation = BaseDatabaseValidation() ## The essential copypasta try: import psycopg2 as Database import psycopg2.extensions except ImportError, e: from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e) DatabaseError = Database.DatabaseError IntegrityError = Database.IntegrityError psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) psycopg2.extensions.register_adapter(SafeString, psycopg2.extensions.QuotedString) psycopg2.extensions.register_adapter(SafeUnicode, psycopg2.extensions.QuotedString) # EOF