--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2B16coM5s7KgGrMF4MqTGY5DYZ%2B7M43-4PkV14sm2yNTvstCHA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.
I only had to query external databases.
I've created my models as managed=False and implemented a custom manager that redefined the default database configuration. We did not had situations where depending on a certain condition the target database would change, but this worked just fine.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGyPVTsLt%2BPcWojAeGJFGqE9oNsR4HA6ecgnYg8zeBU%2BTGFG-g%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.
class ExternalManager(models.Manager):"""Manager customizado para buscar os dados no banco legado."""alias = Nonedef __init__(self, alias=None):self.alias = aliassuper(LegadoManager, self).__init__()def get_query_set(self):"""Retorna um query set conectando ao banco legado."""if not hasattr(self.model, '_db_alias'):self.model._db_alias = self.aliasqs = QuerySet(self.model)if self.model._db_alias is not None:qs = qs.using(self.model._db_alias)return qsNow, create your model and override the objects property, passing the name of the database configured in settings.py as the alias.
Like this:
class ModelInAnotherDatabase(models.Model):
name = models.CharField()
objects = ExternalManager(alias="external")
Where external is the database name configured in settings.py.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2B16coOOX0W6MjxDvB9GRUjq%3D-tYj4bfZX%2B_K1hV1rUt-uMpiA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGyPVTsDVY89bjLP2nkEDQ14vJ8K7xG21riFGj70b2HROm731w%40mail.gmail.com.