Monday, January 2, 2017

Re: multiple databases - two questions

Mike,

Maybe you don't need a custom DB manager.  Perhaps a simple
DB router would suffice?  It's a Django way to specify which DB
to use based on which model is being used, whether it is being
read/written, etc.

See:
- https://docs.djangoproject.com/en/dev/topics/db/multi-db/#topics-db-multi-db-routing

--Fred

Fred Stluka -- mailto:fred@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.

On 11/4/16 12:37 AM, Mike Dewhirst wrote:
(this one might be easier to read)

I am planning some utility helper code to push selected records and their children from our staging database into the production database. The current database would be 'default' and I could add a second one called 'production' then read from 'default' and write to 'production'. (I need to check we are on the staging server).

The docs say ...

https://docs.djangoproject.com/en/1.8/topics/db/multi-db/#using-managers-with-multiple-databases

... but I don't quite understand. There are no custom managers involved; only the out-of-the-box MyModel.objects.

Q1. Should I be using db_manager()?

The plan is to write some utility code like this ...

from substance.models import Substance

def
write_substance_to_production(subst):

    prd_subst, create =
Substance.objects.using('production').get_or_create(

        name=subst.name,

        )

    if create:

        pass

        # copy all subst attributes except id to prd_subst


Q2. Is this a reasonable approach?

Thanks for any hints

Mike


No comments:

Post a Comment