Skip to content
Advertisement

How do I merge two django db’s?

I have two instances of the same Django app. I need to merge the data in these DBs to one DB.

I considered Natural Key fixtures, but I have many objects whose natural key involves fields from a related model, so they are not being serialized when I am serializing using natural keys.

For example

JavaScript

This account’s fixture:

JavaScript

(notice it does not contain it’s natural key, since it is not a part of the model)

What is the best way to merge data from two identical Django apps?

Edit:

To clarify, I have my data, and the instances have different values, but (in some cases) the same primary keys -since they are in different DBs. I want to have all my data (the instances and their related objects) in the same DB.

Advertisement

Answer

I had a similar issue, two identical websites sharing several models but with different objects. Unfortunately, merging them by natural_key was impossible.

I had to implement a new django commands similar to loaddata, in order to append all models from the second website into the first one.

JavaScript

I suggest you to test everything in the django default test DB, with this TestCase. It uses a pre dumped fixture file from the django app (using python manage.py dumpdata) to populate the test DB, after that it applies the custom command to append all object from other database fixture dumps.

JavaScript

Example of test (appending data from Website2 into Website1):

JavaScript

Example of usage (appending data from Website2 into Website1):

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement