由于性能原因,我正在考虑从Postgres将我的网站后端迁移到Mongo,但网站的关键部分依靠GeoDjango模型来计算现实世界中对象之间的距离(等等)。
I'm thinking about shifting my site's backend to Mongo from Postgres for performance reasons, but key parts of the site rely on the GeoDjango models to calculate distances between objects in the real world (and so on).
将大部分网站运行在Mongo上,但使用Postgres进行存储的关键领域是否可行?这是痛苦吗?有没有一个全蒙解决方案我失踪了?
Would it be feasible to have most of the site running on Mongo but those key areas using Postgres for storage? Is this painful and / or error-prone? Is there an all-Mongo solution I'm missing?
任何可以为我解决这些问题的灯光将不胜感激。
Any light you can shed on these matters for me would be much appreciated.
推荐答案自Django 1.2起,您可以在 settings.py 中定义多个datbase连接。然后,您可以使用 数据库路由器 告诉Django哪个数据库要透明地为您的应用程序。
Since Django 1.2, you can define multiple datbase connections in your settings.py. Then you can use database routers to tell Django which database to go to, transparently for your application.
免责声明:这是我认为 em>它应该工作,我从来没有在Django中使用过MongoDB,也没有测试过我的代码实际工作。 :$) settings.py
Disclaimer: this is how I think it should work, I have never used MongoDB in Django, nor have I tested that my code actually works. :)
DATABASES = { 'default': { 'ENGINE': 'django_mongodb_engine', 'NAME': 'mydata', ... } 'geodata' { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'geodata', ... } } DATABASE_ROUTERS = ['path.to.ModelMetaRouter']模型
然后将自定义Meta变量添加到地理表,以覆盖其数据库。不要将此属性添加到应该进入默认数据库的模型。
Models
Then add custom Meta variables to your geo-tables, to override their database. Don't add this attribute to models that are supposed to go to the default database.
class SomeGeoModel(models.Model): ... class Meta: using = 'geodata'数据库路由器
并写入一个数据库路由器,将所有具有元素属性集的模型引导到适当的连接:
Database router
And write a database router to direct all models that have the using meta attribute set, to the appropriate connection:
class ModelMetaRouter(object): def db_for_read(self, model, **hints): return getattr(model._meta, 'using', None) def db_for_write(self, model, **hints): return getattr(model._meta, 'using', None) def allow_relation(self, obj1, obj2, **hints): # only allow relations within a single database if getattr(obj1._meta, 'using', None) == getattr(obj2._meta, 'using', None): return True return None def allow_syncdb(self, db, model): if db == getattr(model._meta, 'using', 'default'): return True return None更多推荐
混合PostgreSQL和MongoDB(如Django后端)
发布评论