【问题标题】:Using postgis and mongodb django使用 postgis 和 mongodb django
【发布时间】:2019-10-26 14:45:58
【问题描述】:

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

我的问题,我尝试将地址模型存储在 postgresql 中,所有其他模型(身份验证、产品...)都使用 djongo 包存储在 mongodb 引擎中。 存储在 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 中。

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

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 新手,感谢您提供任何进一步的帮助

【问题讨论】:

    标签: django mongodb postgresql postgis djongo


    【解决方案1】:

    更新

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

    地理定位.路由器

    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

    【讨论】:

      猜你喜欢
      • 2019-07-06
      • 1970-01-01
      • 2014-04-26
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 2012-08-27
      相关资源
      最近更新 更多