I’m very new in Python and Django programming. Recently I was writing a migration script to copy data across databases. Each database was in different machine and I’ve all the configurations in settings.py under different label like ‘default’, ‘staging’, ‘production’ etc.
While the migration was working almost fine, I was getting an error like the following during saving associated objects.
ValueError: Cannot assign "": instance is on database "staging", value is on database "default"
I was copying objects in the following way:
>>> post = Post.objects.using('staging').get(pk=999) >>> post.pk = None >>> post.save(using='default')
However, I was getting the mentioned error when I was going to assign post in other associated objects (for example: comment).
For debugging, what is actually happening here, I wanted to know the settings which this object actually using. I’ve googled quite sometime and returned with empty hands. So, I started inspecting the objects. Today I found the way. This is quite simple.
>>> post._state.db 'default'
This will tell you the name of the database setting. This is a simple thing but I wanted to share the findings so that you don’t have to waste time like I did for debugging this. More importantly, this will work as a personal note for my future usage :).