Let's JAM!

Error when importing a DB router in Django

DB routers in Django allow for complex DB set ups. For example, reading from one database and writing to another or using a different database for a specific model.

 

I had created a router and placed it in my app's models.py file. I edited the DATABASE_ROUTERS setting in settings.py. Yet no dice, I kept getting the same error over and over. The error I got was: 

django.core.exceptions.ImproperlyConfigured: Error importing database router DBRouter: "cannot import name connection"

 

Not too helpful, even after searching around on the web. So I started exploring. After an hour or so of trying to figure it out, I gave up until tomorrow. Tomorrow came and I decided to try moving it from the models.py file into a new file named routers.py. Eureka! All is well!