Nephie
Nephie

Reputation: 67

Django - how to copy one, specific model instance between two databases

I have an application that has a model called CalculationResults. I also have two databases with the same scheme - let's call them "source" and "target". On source database, I have a specific instance of CalculationResults that I want to migrate to "target" database - preferably, I also want to change the field "owner" of this instance in the process. What is the easiest way to achieve this goal? The data I'm talking about is not huge, so it's rather a question of manpower vs computational power.

Upvotes: 0

Views: 171

Answers (1)

Egor Wexler
Egor Wexler

Reputation: 1944

I have never done this, but I believe that following will work:

results = CalculationResults.objects.using('source').filter(field=search_field)

for result in results:
    result.owner = 'new owner'
    result.save(using='target')

Upvotes: 1

Related Questions