planet260
planet260

Reputation: 1454

Django -- Multiple Databases -- Data Migration

I am having difficulty understanding Django Multiple Databases documentation. Below is what I am trying to achieve.

I have to migrate some data from one database to another in Python. Both databases have same structure, so I only have one model file.

What I need to do in code is select data from some tables of one database and insert into tables of another database.

How can I do it, i.e., select in Model query which database to use? Also any suggestions and recommendation would be appreciated.

Thanks

Upvotes: 0

Views: 282

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77902

The documentation here https://docs.djangoproject.com/en/1.6/topics/db/multi-db/#manually-selecting-a-database is quite clear.

Assuming you have "db_for_read" and "db_for_write" configured in your settings, for the reading:

YourModel.objects.using("db_for_read").all()

For the writing - per instance:

your_model_instance.save(using="db_for_write")

or in batch:

YourModel.objects.using("db_for_write").bulk_create(
    [your_model_instance1, your_model_instance2, etc]
    )

Upvotes: 1

Related Questions