2013-05-21 23 views
0

我想在我的Django项目中设置很多数据库。 这是我的settings.py:在Django中的多数据库

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'] 

我知道,我使用Postgres的用户。它是开发者。

而且我有这个router.py在portail文件夹:

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

如果我试图让:

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

我有一个错误,我的表不存在。我想它不能确定哪个数据库。

我知道我的路由器正在执行。这很奇怪,如果我在django(默认和osm)中将我的两个数据库“portail”和“osm”换成同名,它仍然不起作用。而且我知道Django正在进入“如果”。

一些意见后,我给你我的观点:

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) FROM planet_osm_line WHERE osm_id= %s UNION SELECT ST_GeometryType(way) FROM planet_osm_polygon WHERE 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") 

我的一个模型(PlanetOsmLine,PlanetOsmPoint或PlanetOsmPolygon是相同的):

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 to remove unsuitable characters. 
    access = models.TextField(blank=True) 
    addr_housename = models.TextField(db_column='addr:housename', blank=True) # Field renamed to remove unsuitable characters. 
    addr_housenumber = models.TextField(db_column='addr:housenumber', blank=True) # Field renamed to remove unsuitable characters. 
    addr_interpolation = models.TextField(db_column='addr:interpolation', blank=True) # Field renamed to 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, the list is 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) # This field type is a guess. 
    way = models.GeometryField(blank=True,srid=3857) # This field type is a 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 

问候

+1

你有没有在http://127.0.0.1:8000/api/way/96300215地址是什么浏览功能?你能发布它的代码吗? –

+1

@ etienne-trimaille,你已经验证它正在从db_for_read()返回所需的输出吗?您可以通过检查模型管理器上的db属性来执行此操作。要么我们看到你的视图(也许是模型)代码,或者用'./manage.py runserver_plus --print-sql'尝试django-extensions。 –

+0

@VasilyAlexeev我添加了我的视图和我的模型。 Django扩展似乎非常有用,但我在安装时遇到了一些困难(具有依赖性)。我再试一次。 – Gustry

回答