使用 postgis 和 mongodb django

Using postgis and mongodb django

我使用 django (2, 1, 5, 'final', 0).

我的问题,我尝试在 postgresql 中存储一个地址模型,在 mongodb 引擎中使用 djongo 包存储所有其他模型(auth、products...)。 存储在 app => geolocalise 中的模型:

class Address(models.Model):

    line1 = models.CharField(max_length=150)
    line2 = models.CharField(max_length=150, blank=True)
    postalcode = models.CharField(max_length=10)
    city = models.CharField(max_length=150)
    country = models.CharField(max_length=150, default="France")
    #the postgis entry
    location = models.PointField(null=True)
    latitude = models.FloatField( default=0)
    longitude = models.FloatField( default=0)
    description = models.TextField(max_length=1024,blank=True)
    #user = models.ForeignKey('User', related_name='user', on_delete=models.CASCADE)

    def __str__(self):
        return "{0} - {1} - {2}".format(self.line1, self.postalcode, self.city)

这里是我的设置py

DATABASES = {
'default': {
  'ENGINE': 'djongo',
  'NAME': 'DBNAME',
  'USER': 'USER',
  'PASSWORD': 'PASSWORD'},

'postgresql': {
  'ENGINE': 'django.contrib.gis.db.backends.postgis',
  'NAME': 'DBNAME',
  'USER': 'USER',
  'PASSWORD': 'PASSWORD'}} 

但是当我进行迁移时,我不知道为什么数据库存储在 mongodb。

我尝试了 router.py 中的配置,我在 Whosebug 上找到了它,但它不起作用:

class GeolocaliseRouter(object):
def db_for_read(self, model, **hints):
    """
    Attempts to read auth models go to auth_db.
    """
    if model._meta.app_label == 'geolocalise':
        return 'postgresql'
    return None

def db_for_write(self, model, **hints):
    """
    Attempts to write auth models go to auth_db.
    """
    if model._meta.app_label == 'geolocalise':
        return 'postgresql'
    return None

def allow_relation(self, obj1, obj2, **hints):
    """
    Allow relations if a model in the auth app is involved.
    """
    if obj1._meta.app_label == 'geolocalise' or \
       obj2._meta.app_label == 'geolocalise':
       return True
    return None

def allow_migrate(self, db, app_label, model_name=None, **hints):
    """
    Make sure the auth app only appears in the 'auth_db'
    database.
    """
    if app_label == 'geolocalise':
        return db == 'postgresql'
    return None

我已经测试了多种配置,但无论何时我进行迁移,模型都在 mongodb 中,或者我项目的所有模型都在 postgresql 中....

我是 django 的新手,感谢您的进一步帮助

更新

所以经过多次搜索,我可以使用 2 个路由器设置迁移

geolocalise.router

geolocalise = ['geolocalise']

class GeolocaliseRouter(object):

    def db_for_read(self,model, **hints):
        if model._meta.app_label in geolocalise:
            return 'postgresql'
        return None

    def db_for_write(self,model, **hints):
        if model._meta.app_label in geolocalise:
            return 'postgresql'
        return None

    def allow_relation(self,obj1, obj2, **hints):
        if obj1._meta.app_label in geolocalise and \
           obj2._meta.app_label in geolocalise:
           return True
        return None

    def allow_syncdb(self,db, model):
        if db == 'postgresql':
            if model._meta.app_label in geolocalise:
                return True
        elif model._meta.app_label in geolocalise:
            return False
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in geolocalise:
            return db == 'postgresql'
        return None

和 mongo

app = ['api','admin', 'booking', 'categories', 'notification', 'promocodes', 'publicity', 'reviews', 'space','transactions']


class DefaultRouter(object):

    def db_for_read(self,model, **hints):
        if model._meta.app_label in app:
            return 'default'
        return None

    def db_for_write(self,model, **hints):
        if model._meta.app_label in app:
            return 'default'
        return None

    def allow_relation(self,obj1, obj2, **hints):
        if obj1._meta.app_label in app and \
           obj2._meta.app_label in app:
           return True
        return None

    def allow_syncdb(self,db, model):
        if db == 'default':
            if model._meta.app_label in app:
                return True
        elif model._meta.app_label in app:
            return False
        return None


    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in app:
            return db == 'default'
        return None

通过添加

更新setting.py
DATABASE_ROUTERS = ['backend.router.DefaultRouter', 'geolocalise.router.GeolocaliseRouter', ]

编辑

我的迁移是通过此命令应用的:

python3 manage.py migrate geolocalise --database postgresql

但我的外键无法正常工作,因为 django 无法处理两个不同数据库之间的关系...一天就没了。

更多信息在这里:Unable to save with save_model using database router