How can I update an already populated Django database with records from a data fixture (or other serialized records)?
I know I can use Django data fixtures to provide initial data. Can I use the same already written features to update the database from data fixtures (or similar serialized data like a JSON document)?
The “insert or update from serialised data” operation should be idempotent:
- If a record doesn't exist (by its key) in the database, it should be inserted.
- If a record already exists (by its key) in the database, it should be updated to match the data fixture.
- The end state should be that all data from the data fixture should be updated in the database, no matter if the records already existed.
Specifically, can I update existing rows by specifying pk=null
and using natural keys?
How can I use the existing Django “load data” features (whether loaddata
or something else similar in Django), to read serialised data, insert records if they don't exist and update them if they already exist?