Tuesday, February 2, 2016

Re: Unintuitive behaviour of management commands with multiple databases

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)

iQIcBAEBCAAGBQJWsOJ2AAoJEC0ft5FqUuEhLgIP/iJQmuOKJuRJ4c9HJIPJDbDr
eOxumm8PiPfQszxPCxKrLlM+AmA/Nt2cHIUMxhcJ74DYm32S8mxrJVAJStEf/SZ9
b8rgEJ2ry4JA2Q+FDrJXVEJ9Sn2/O3Xws6IOLDuC2M7DsRfckLWm7JrtG+Z8ijPs
28ZnvIQ3D9gu8yhC+8X2J/9oxLFGSydOkTeDli/Q8YwRTNSSaENJk60qIN/04HhU
iwyfDg+EwrljqCx2ZwyG3kfPFbIlP2zV2oJxtDEJnP2QpH/8s8yAWlGJgcfnNJ6g
CbV4UYvMhjRAH8LVO2lKJ5K8eWB/7WRajEhQGwDpHLdD+CsdF9Ae9WK9VYC9JYJR
19XBrnaqBf3Gn04PsiNV35HuYXXAZdlt9UFE6PGr6zYWFyCpk5BaVsn9dTGVj6HL
Cd/YhTBuJZeZhpC0P1A1yuWw1WTeodDNYwxZN+DlXMu1FXzvgG0FBC8GzwSKusvV
eSIdM+od39Y2C8i1H9S2Uk6DyO5hAhaQqlZ8zoDmBZUk/TfLMCGmBUuivqEGMfPI
o3XeEV69hEfzDJxx+UmIJ5F7Wj74ghBK/K08e0cUOpOJ4c7CDgVp8uzR9/dA4yNA
mmcZLCmvs8twnXJygPScS0npIrQNVTxCVZoqI8x+aB7mKKUDRaJ4KmiAOTJSt1zB
ffa/fKxV8XzEJMfXv8JG
=G6yR
-----END PGP SIGNATURE-----
Hi Vinay,

On 02/02/2016 09:52 AM, 'Vinay Sajip' via Django users wrote:
>
> On Tuesday, February 2, 2016 at 3:27:10 PM UTC, Carl Meyer wrote:
>
> Nothing in your code ever "overrides" settings.DATABASES['default'].
>
>
> Dear Carl,
>
> Thanks for the quick response. I /had/ thought of that, and tried adding
> the statement settings.DATABASES['default'] = settings.DATABASES[alias]
> just before the with block, and it had no effect - the result was the
> same - so I took it out. How else are you supposed to override the
> default database?

You can't (well, you might be able to by poking around in the internals
of the django.db.connections object, but I'd strongly advise against
that). The proper (and thread-safe) way to achieve the equivalent is to
do what the third-party project I linked (django-dynamic-db-router)
does: write a db router that references some thread-local state to
determine which database to route queries to, and write a context
manager or whatever to modify that thread-local state. Or just use
django-dynamic-db-router, since it already does what you want :-)

Carl

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/56B0E273.8070604%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment