> I have several installed applications within my Hue environment. I'd like
> to use the multiple database support to route all database requests
> associated with apptwo.models into a database called "apptwo", while keeping
> everything else in the default database. Is this possible? It seems like
> I'd have to do quite a bit of work to write a custom router, whereas this
> seems like a pretty common case.
The database router is the right tool for this job (the only one, actually).
It might seem like "quite a bit of work," but it's actually very
simple. There's even an example in the docs
(https://docs.djangoproject.com/en/dev/topics/db/multi-db/#an-example)
that does almost exactly what you want. In a nutshell, something like
the following should do the trick::
def app_label(model):
"""Shortcut for getting a model's app_label"""
return model._meta.app_label
class AppTwoRouter(object):
def db_for_read(self, model, **hints):
if app_label(model) == 'apptwo':
return 'apptwo'
return None
db_for_write = db_for_read
def allow_relation(self, obj1, obj2, **hints):
if app_label(obj1) == app_label(obj2) == 'apptwo':
return True
return None
def allow_syncdb(self, db, model):
if db == 'apptwo':
return app_label(model) == 'apptwo'
elif app_label(model) == 'apptwo':
return False
return None
Jacob
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment