Let me explain this better.
I've created a custom manager, in which I replaced the default database
alias it should connect.
class ExternalManager(models.Manager):
"""
Manager customizado para buscar os dados no banco legado.
"""
alias = None
def __init__(self, alias=None):
self.alias = alias
super(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.alias
qs = QuerySet(self.model)
if self.model._db_alias is not None:
qs = qs.using(self.model._db_alias)
return qs
Now, 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.
On Fri, Nov 8, 2013 at 3:11 PM, Ovnicraft <[email protected]> wrote:
>
>
>
> On Fri, Nov 8, 2013 at 11:39 AM, George Silva <[email protected]>wrote:
>
>> 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.
>>
>
> What database configuration did you change ?
>
> Regards,
>
>>
>>
>> On Fri, Nov 8, 2013 at 12:45 PM, Ovnicraft <[email protected]> wrote:
>>
>>> Hi to all friends !
>>>
>>> I am working in django project its has many apps with their default db
>>> configured.
>>> So now i need to read (and possible write) to third DB (mysql) and i
>>> found a thread[1] here about it.
>>>
>>> There are some suggestions about write ReadOnly objects, redefine save
>>> methods but i'm not totally convinced about it.
>>>
>>> Reading documentation about multidb i found DB routes and it has
>>> allow_syndb[2] so my question is, if i code to dont allow sync any model in
>>> third db i can query (RAW SQL) without problems ?
>>>
>>> I dont want to write any dirty layer to connect to my third DB just
>>> re-use django engine.
>>>
>>> I will appreciated your help !
>>>
>>> Best regards,
>>>
>>> [1]
>>> https://groups.google.com/forum/#!msg/django-users/rv6D5Pqs7Fo/pkwinU7PGOQJ
>>> [2]
>>> https://docs.djangoproject.com/en/1.4/topics/db/multi-db/#allow_syncdb
>>>
>>>
>>> --
>>> Cristian Salamea
>>> @ovnicraft
>>>
>>> --
>>> 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 [email protected].
>>> To post to this group, send email to [email protected].
>>> 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.
>>>
>>
>>
>>
>> --
>> George R. C. Silva
>> SIGMA Consultoria
>> ----------------------------
>> http://www.consultoriasigma.com.br/
>>
>> --
>> 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 [email protected].
>> To post to this group, send email to [email protected].
>> 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/CAGyPVTsLt%2BPcWojAeGJFGqE9oNsR4HA6ecgnYg8zeBU%2BTGFG-g%40mail.gmail.com
>> .
>>
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
>
> --
> Cristian Salamea
> @ovnicraft
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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%2B16coOOX0W6MjxDvB9GRUjq%3D-tYj4bfZX%2B_K1hV1rUt-uMpiA%40mail.gmail.com
> .
>
> For more options, visit https://groups.google.com/groups/opt_out.
>
--
George R. C. Silva
SIGMA Consultoria
----------------------------
http://www.consultoriasigma.com.br/
--
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 [email protected].
To post to this group, send email to [email protected].
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/CAGyPVTsDVY89bjLP2nkEDQ14vJ8K7xG21riFGj70b2HROm731w%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.