In some of the previous version on Django I had a very useful context manager.
from contextlib import ContextDecorator
from django.db import connections
class overwrite_default_connection(ContextDecorator):
prev_default = None
write_connection = None
def __init__(self, write_connection):
self.write_connection = write_connection
def __enter__(self):
self.prev_default = connections['default']
connections['default'] = connections[self.write_connection]
return self
def __exit__(self, *exc):
connections['default'] = self.prev_default
return False
Which allows me to overwrite default connection. It is very useful when you use a jupyter notebook and you need to switch between db connections that have different data but the same structure (django models)
But the shared implementation of that context manager stops working recently
ValueError: Subqueries aren't allowed across different databases. Force the inner query to be evaluated using `list(inner_query)`.
But I'm wondering wouldn't it be useful to have that kind of manager in the core Django functionality, or it is a stupid idea?
Thank you