擅长:python、mysql、java
<p>我将所有表分布在动态postgres表模式中,并使用连接信号来设置连接的搜索路径,因为django不支持postgres模式。在</p>
<p>在<strong><code>myapp/apps.py</code></strong></p>
<pre><code>from django.db.backends.signals import connection_created
class MyappConfig(AppConfig):
name = 'myapp'
def ready(self):
from myapp.schema_manager import new_connection
connection_created.connect(new_connection)
</code></pre>
<p>在<strong><code>myapp/schema_manager.py</code></strong></p>
^{pr2}$
<p>根据<a href="https://docs.djangoproject.com/en/1.11/ref/signals/#connection-created" rel="noreferrer">the docs</a>,此信号接收两个参数:</p>
<blockquote>
<p><strong>sender</strong></p>
<p>The database wrapper class – i.e. django.db.backends.postgresql.DatabaseWrapper or django.db.backends.mysql.DatabaseWrapper, etc.</p>
<p><strong>connection</strong></p>
<p>The database connection that was opened. This can be used in a multiple-database configuration to differentiate connection signals from different databases. </p>
</blockquote>