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

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/ad453105-2116-40c9-a1ff-9571bfb080d1n%40googlegroups.com.
  • Fea... Alexander Lyabah
    • ... 'Adam Johnson' via Django developers (Contributions to Django itself)
      • ... Alexander Lyabah

Reply via email to