django sync db to get two apps in two databases - django

I'm not sure how it works but I need two different models/apps in separated databases. So if I try syncdb --database db1 will get all installed apps into db1. So question is how to get just one app in another db?
Thanks

You need to write a database router which will filter models based on application and database in its allow_syncdb method. You cas start from the AuthRouter example in the documentation.

Related

Django transfer database to different Django project without messing up the logic

I built a Django project with a few models. Now I created a second server with the same setup. This one is meant to be the deployment server. Databases are separate from the dev-server.
However can you tell me if I can simply copy the databases from the dev server to the deploy or will the Django logic, since I also mean the user models and permissions etc.
The tables which I created myself are no problem to transfer to the new server. However I am wondering If Django gets confused when I also transfer something like the auth_user Model.
Should this work since I also just copied the backend logic as well?

Migrate users from database to another on Django

I already have an existing website running and want to transfer just the users (usernames, passwords and emails) to another database for a separate Django website as I am rebuilding it.
You can configure both databases in your django configuration file. select the user data from one database and then put the records in the other.
Multiple Databases in Django.
Moving an object from one database to another

Using DB without modifying it - Django

I need to connect to a Postgresql server in my Django project. But I'm under strict instructions NOT to make any modifications to the database or tables.
If I'm not wrong, simply adding this DB to settings.py and running syncdb would create and add some django specific tables to the database.
How can I get around this issue? If I add another (local) sqlite DB to settings.py under default and my remote postresql server separately; can I be sure of Django not creating any tables in my server? I couldn't find this info in the documentation.
Thanks
Configure two databases in settings.py, and a create database router to route your queries to the appropriate database.
Route all the Django admin / users stuff to your sqlite database (make it the default database, and make sure that is indeed the one your router returns by default), and single out your application queries that need to go to the Postgres database.
Routers also have a method to locate a DB for writes and one for reads, so you can use this as a failsafe: just make sure db_for_write never returns your Postgres database, and you're good to go.

Django - Why doesn't syncdb respect the database router?

I have set up a database router to direct different apps and different models to different databases using the db_for_read and db_for_write router methods.
That works very well, except that ./manage.py syncdb does't respect those router settings.
When I syncdb my models, all of them are created in the default database.
The database router only provides an allow_syncdb method, but no sync_to method. Is there a way to tell the syncdb command where to create the new tables?
Note: I can't use the --database feature, as sometimes some of the model apps go to a different database than the rest of the app.
When you write your router make sure you've written the allow_syncdb() method. It takes both a database and a model. When you run manage.py syncdb you're essentially setting the --database=default. If you don't want your models to sync to the default database then your allow_syncdb() method should return False for the condition that db==default and model._meta.app_label==myapp.
You'll need to run syncdb with the --database=your_other_db option to get myapp into that db. But make sure in that case that allow_syncdb() returns True only for the case that db==your_other_db and model._meta.app_label==myapp.
Does that make sense? Basically you have to run the manage.py syncdb method twice, once for each database. You cannot run it only once and have it update both databases.

Tying a model to a specific database alias

I have defined two databases in my settings.py with default, cas.
In my accounts application models.py I have created two clases
UserProfile and Users.
I want to tie UserProfile table to default and Users to cas db setting:
so for e.g. when I do a syncdb using the following command
python manage.py syncdb --database=cas
it should create only the users table in CAS and not the UserProfile table too.
Is there a way I can achieve this?
Take a look at the Database routing features of Django 1.2. You'll likely find what you need:
http://docs.djangoproject.com/en/dev/topics/db/multi-db/#automatic-database-routing