With Django multidb, it's fairly easy to write a router that runs a master/slave infrastructure. But is it possible to write a router that writes to multiple databases? My use case is a collection of projects, all running on the same domain. To save users from registering/login in on every site, I'd like to synchronize the contrib.auth
and contrib.sessions
tables. Is that possible with Django multidb or should I look into replication features of the database system (MySQL in my case)?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Django __str__ returned non-string (type NoneType)
- Evil ctypes hack in python
i think you will be better implementing an SSO or OAuth service
but if you want like to synchronize your table users between two database and if you are using your own UserModel you can do something like this
you can also putting with a decorator like this, like this you can also use it for synchronizing other tables:
Hope this will help :)
I'm working on a Django sharding schema right now.
i looked at the Django router but decided to roll my own.
some thoughts on your issue:
something like--
saving User objects (at least on a single-DB model) seems to save session, auth, etc. data under the hood via the various magic occurring inside django.contrib, so there's a chance you might not have to go in and figure out the names and types of all these database tables.
in support of the possibility of this working, I swear I read somewhere recently (probably on one of Alex Gaynor's blog posts) that if an object has a foreign key Django will attempt to use the same DB the object lives on (for obvious reasons in light of the way Django typically operates).
from the example on the Django multiDB page you referenced I wonder if something like the following would work:
their sample code:
a possible modification:
def allow_syncdb(self, db, model):
looking at it again, this code would probably be more useful as the "db_for_write" function. but you get the idea.
there are no doubt other types of model you would have to add to make this work (all the auth stuff, which is extensive).
good luck! hope this is helpful in some way.
i'm interested in your findings and comments!
jb
First, I think what you need is more an SSO framework, like in this post
I tried mouad's answer, but I couldn't get class decorator working ... And it seems to me this solution does not allow to have custom save() in models.
More appropriate to my need, I've defined a custom generic class and simply override the save() function.
And then: