The accepted solution for Ticket 6064 does not seem to address the
original problem—initializing a *specific* connection—because it uses
the the class instead of the instance as the sender of the signal. I
attempted to reopen the ticket but it was closed and I was told to
take it up on the users mailing list. That didn't seem like the right
place, but I did anyway and got no response. Perhaps this is the right
place. I'm attaching a change that uses the ``connection``, instead of
the ``__class__``, as the sender of the signal. If there is something
wrong with the changes, I'd be more than happy to revise it, but I'd
like to have this functionality in Django proper, since it makes my
particular use case much easier.

My Use Case
==========

A multitenant application where each tenant has a completely isolated
set of data. To avoid having to add some sort of tenant foreign key on
every table and filter on it on every request, I'm using a different
Postgres schema for each tenant. Therefore, I need to initialize the
connections I create with a different search_path depending on which
user made the request.

The Changes
==========

Index: django/db/backends/postgresql/base.py
===================================================================
--- django/db/backends/postgresql/base.py       (revision 11468)
+++ django/db/backends/postgresql/base.py       (working copy)
@@ -115,7 +115,7 @@
                 conn_string += " port=%s" % settings_dict
['DATABASE_PORT']
             self.connection = Database.connect(conn_string,
**settings_dict['DATABASE_OPTIONS'])
             self.connection.set_isolation_level(1) # make
transactions transparent to all cursors
-            connection_created.send(sender=self.__class__)
+            connection_created.send(sender=self.connection)
         cursor = self.connection.cursor()
         if set_tz:
             cursor.execute("SET TIME ZONE %s", [settings_dict
['TIME_ZONE']])
Index: django/db/backends/sqlite3/base.py
===================================================================
--- django/db/backends/sqlite3/base.py  (revision 11468)
+++ django/db/backends/sqlite3/base.py  (working copy)
@@ -172,7 +172,7 @@
             self.connection.create_function("django_extract", 2,
_sqlite_extract)
             self.connection.create_function("django_date_trunc", 2,
_sqlite_date_trunc)
             self.connection.create_function("regexp", 2,
_sqlite_regexp)
-            connection_created.send(sender=self.__class__)
+            connection_created.send(sender=self.connection)
         return self.connection.cursor(factory=SQLiteCursorWrapper)

     def close(self):
Index: django/db/backends/mysql/base.py
===================================================================
--- django/db/backends/mysql/base.py    (revision 11468)
+++ django/db/backends/mysql/base.py    (working copy)
@@ -281,7 +281,7 @@
             self.connection = Database.connect(**kwargs)
             self.connection.encoders[SafeUnicode] =
self.connection.encoders[unicode]
             self.connection.encoders[SafeString] =
self.connection.encoders[str]
-            connection_created.send(sender=self.__class__)
+            connection_created.send(sender=self.connection)
         cursor = CursorWrapper(self.connection.cursor())
         return cursor

Index: django/db/backends/oracle/base.py
===================================================================
--- django/db/backends/oracle/base.py   (revision 11468)
+++ django/db/backends/oracle/base.py   (working copy)
@@ -331,7 +331,7 @@
                 # Django docs specify cx_Oracle version 4.3.1 or
higher, but
                 # stmtcachesize is available only in 4.3.2 and up.
                 pass
-            connection_created.send(sender=self.__class__)
+            connection_created.send(sender=self.connection)
         if not cursor:
             cursor = FormatStylePlaceholderCursor(self.connection)
         return cursor
Index: django/db/backends/postgresql_psycopg2/base.py
===================================================================
--- django/db/backends/postgresql_psycopg2/base.py      (revision 11468)
+++ django/db/backends/postgresql_psycopg2/base.py      (working copy)
@@ -98,7 +98,7 @@
             self.connection = Database.connect(**conn_params)
             self.connection.set_client_encoding('UTF8')
             self.connection.set_isolation_level(self.isolation_level)
-            connection_created.send(sender=self.__class__)
+            connection_created.send(sender=self.connection)
         cursor = self.connection.cursor()
         cursor.tzinfo_factory = None
         if set_tz:

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to