0

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

1 Answer 1

1

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]
    )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.