I whould like to use postgreSQL schemas with django, how can I do this?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Django __str__ returned non-string (type NoneType)
- Evil ctypes hack in python
For SQL server database:
As mentioned in the following ticket: https://code.djangoproject.com/ticket/6148, we could set
search_path
for the django user.One way to achieve this is to set
search_path
viapsql
client, likeThe other way is to modify the django app, so that if we rebuild the database, django won't spit all the tables in
public
schema.To achieve this, you could override the
DatabaseWrapper
defined indjango.db.backends.postgresql_psycopg2.base
Create the following directory:
Here's the content of
base.py
In
settings.py
, add the following database configuration:I know that this is a rather old question, but a different solution is to alter the SEARCH_PATH.
Example
Lets say you have three tables.
schema1.table_name_a
schema2.table_name_b
table_name_c
You could run the command:
And refer to the tables by their table names only in django.
See 5.7.3. The Schema Search Path
It's a bit more complicated than tricky escaping. Have a look at Ticket #6148 in Django for perhaps a solution or at least a patch. It makes some minor changes deep in the django.db core but it will hopefully be officially included in django. After that it's just a matter of saying
in the Meta class or for a global change set
in settings.py
UPDATE: 2015-01-08
The corresponding issue in django has been open for 7 years and the patch there will not work any more. The correct answer to this should be...
At the moment you can't use postgreSQL schemas in django out of the box.
I've been using:
in the past without realising that only work for read-only operation. When you try to add new record it would fail because the sequence would be something like "schema.tablename"_column_id_seq.
does work so far. Thanks.
Maybe this will help.
I get the answer from the following link: http://blog.amvtek.com/posts/2014/Jun/13/accessing-multiple-postgres-schemas-from-django/