Django Database Routers Specifying different databases in code

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

The normal obj.save() method will use the default database, or if a database router is used, it will use the database as specified in db_for_write. You can override it by using:

obj.save(using='other_db')
obj.delete(using='other_db')

Similarly, for reading:

MyModel.objects.using('other_db').all()


Got any Django Question?