python - Multi-database in Django -


i'm trying setup many databases in django project. settings.py :

    databases = {     'default': {         'engine': 'django.contrib.gis.db.backends.postgis',         'name': 'portail',         'user': 'postgres',         'password': '',         'host': '',         'port': '',     },     'osm': {         'engine': 'django.contrib.gis.db.backends.postgis',         'name': 'osm',          'user': 'postgres',         'password': '',         'host': '',         'port': '',     } }  database_routers = ['portail.router.dbrouter'] 

i know, i'm using postgres user. it's dev.

and have router.py in portail folder :

class dbrouter(object):      def db_for_read(self, model, **hints):         if model._meta.app_label == 'api':             return 'osm'         return 'default' 

if try :

http://127.0.0.1:8000/api/way/96300215 

i have error, table not exist. suppose can't determine db.

i know router executed. it's strange, if switch 2 databases "portail" , "osm" same name in django (default , osm) still doesn't work. , know django going "if".

after comments, give view :

def getobject(request,type,id,format):      if type == "node":         osmobject = get_object_or_404(planetosmpoint,osm_id=id)          if type == "way" or type == "relation":         if type == "relation":             id = int(id) * -1          #un way|relation peut-être dans la table line ou polygon, il faut tester avec un union et récuperer le type de géometrie         cursor = connection.cursor()         cursor.execute('select st_geometrytype(way) planet_osm_line osm_id= %s union select st_geometrytype(way) planet_osm_polygon osm_id= %s',[id,id])          #si plusieurs résultats, erreur !         if cursor.rowcount != 1:             print cursor.rowcount             raise http404      osmtype = cursor.fetchone()          if osmtype[0] == u'st_polygon':             osmobject = get_object_or_404(planetosmpolygon,osm_id=id)         elif osmtype[0] == u'st_linestring':             osmobject = get_object_or_404(planetosmline,osm_id=id)         else:             raise http404       if format == '' or format == "geojson" or format == "json":         return httpresponse(osmobject.way.geojson, content_type="application/json") 

one of model (planetosmline, planetosmpoint or planetosmpolygon same) :

class planetosmline(models.model):     osm_id = models.bigintegerfield(primary_key=true)     school_cm = models.textfield(db_column='school:cm', blank=true) # field name made lowercase. field renamed remove unsuitable characters.     access = models.textfield(blank=true)     addr_housename = models.textfield(db_column='addr:housename', blank=true) # field renamed remove unsuitable characters.     addr_housenumber = models.textfield(db_column='addr:housenumber', blank=true) # field renamed remove unsuitable characters.     addr_interpolation = models.textfield(db_column='addr:interpolation', blank=true) # field renamed remove unsuitable characters.     admin_level = models.textfield(blank=true)     aerialway = models.textfield(blank=true)     aeroway = models.textfield(blank=true)     amenity = models.textfield(blank=true)     #some other fields, list quite long     wetland = models.textfield(blank=true)     width = models.textfield(blank=true)     wood = models.textfield(blank=true)     z_order = models.integerfield(null=true, blank=true)     way_area = models.floatfield(null=true, blank=true)      #way = models.linestringfield(blank=true,srid=3857) # field type guess.     way = models.geometryfield(blank=true,srid=3857) # field type guess.     objects = models.geomanager()      def __unicode__(self):         return str(self.osm_id)      class meta:         db_table = 'planet_osm_line'         verbose_name_plural = "planet_osm_line"         managed = false 

regards

i solved problem. cursor connection. in view, add :

from django.db import connections #cursor = connection.cursor() cursor = connections['osm'].cursor() 

now, connection know database. router useless in case.

source : https://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -