What I did is create an attribute in my models and called it
model.py
class Data(models.Model):
# connection_name is my database name which points to the name from
settings.py
connection_name = "gis"
name = models.CharField(max_length=50)
area = models.IntegerField()
and created a router.py
class DbRouter(object):
"""A router to control all database operations on models in
the contrib.auth application"""
def db_for_read(self, model, **hints):
if hasattr(model,'connection_name'):
return model.connection_name
return None
def db_for_write(self, model, **hints):
if hasattr(model,'connection_name'):
return model.connection_name
return None
def allow_syncdb(self, db, model):
if hasattr(model,'connection_name'):
return model.connection_name == db
return db == 'default'
so, everytime for read or write operation it will check the model for
attribute connection_name and if not found it will use the default.
hope this will help,
cheers,
On Fri, Sep 2, 2011 at 1:23 PM, Kevin <[email protected]> wrote:
> The only solution I can directly think of, since the SITE_ID is in
> settings.py would be to use some python logic to determine the
> database for that site.
>
> eg.
> if SITE_ID == 1:
> ... Database settings for site 1 here ...
>
> Use an if-then like this in your settings.py.
>
> Now if what your going after is storing the SITE_ID in a user's
> profile and using that for routing, this will be more complex, as you
> will need to code a new databasebackend to perform the routing, and
> perhaps some middleware and an authbackend.
>
> Mind you, I haven't done much work on multiple databases, so these are
> just ideas on how I think it might be done.
>
> On Sep 1, 9:54 pm, Terribyte <[email protected]> wrote:
> > I had a kind of crazy idea and I wanted to bounce it off of some
> > people with a lot more django experience than I.
> >
> > Here's my scenario...
> >
> > I would like 1 code base, this code base services potentially hundreds
> > of businesses, each of which I want to have a copy of the same schema
> > but with data only relevant to that business.
> >
> > Basically my idea is the following
> > If the model is a Site object or User object, route to the default
> > database, otherwise route reads and writes to a database name I'll set
> > in the sites table.
> >
> > The trick is I need to set the SITE_ID for the current request, and
> > I'm not sure exactly When that should happen (after login of course),
> > or if this idea is even workable.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.