I use django (2, 1, 5, 'final', 0).
My problem, i try to store an address model in postgresql, all the others models (auth, products...) in mongodb engine with the djongo package. the models stored in 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)
here my settings 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'}}
But when i do my migrations i don't know why the database is store in mongodb.
I've try a configuration in a router.py which i found on stackoverflow but it's isn't working:
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
I've tested multiple configuration but anytimes i do the migration the model is in mongodb or all the models of my project go in postgresql....
I'm new in django and thank you for any futher assistance
UPDATE
So after many search i can set the migration with 2 router
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
and for 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
update the setting.py by adding
DATABASE_ROUTERS = ['backend.router.DefaultRouter', 'geolocalise.router.GeolocaliseRouter', ]
EDIT
My migration is apply by this command:
python3 manage.py migrate geolocalise --database postgresql
But my foreign key is not working properly cause django doesn't handle relations between two differents databases... One day lost.
more infos here: Unable to save with save_model using database router