1

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

我的问题,我尝试将地址模型存储在 postgresql 中,所有其他模型(身份验证、产品...)在 mongodb 引擎中使用 djongo 包。存储在 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 的新手,感谢您提供任何进一步的帮助

4

1 回答 1

1

更新

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

地理定位.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 不能处理两个不同数据库之间的关系......有一天迷路了。

更多信息:无法使用数据库路由器使用 save_model 保存

于 2019-10-26T16:17:30.153 回答