[英]Multi-database in Django
I'm trying to setup many databases in my Django project. 我正在尝试在Django项目中设置许多数据库。 This my settings.py :
这是我的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. 我知道,我正在使用postgres用户。 It's dev.
是开发人员
And I have this router.py in portail folder : 我在portail文件夹中有这个router.py:
class dbrouter(object):
def db_for_read(self, model, **hints):
if model._meta.app_label == 'api':
return 'osm'
return 'default'
If I try to get : 如果我尝试获得:
http://127.0.0.1:8000/api/way/96300215
I have an error, my table does not exist. 我有一个错误,我的表不存在。 I suppose that it can't determine which DB.
我想它不能确定哪个数据库。
I know that my router is executed. 我知道我的路由器已执行。 It's very strange, if I switch my 2 databases "portail" and "osm" with the same name in django (default and osm) it still doesn't work.
这很奇怪,如果我在django(默认值和osm)中用相同的名称切换了两个数据库“ portail”和“ osm”,它仍然不起作用。 And I know that Django is going into the "if".
而且我知道Django将进入“ if”状态。
After some comments, I give you my 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) 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")
One of my model (PlanetOsmLine, PlanetOsmPoint or PlanetOsmPolygon are the same) : 我的模型之一(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
Regards 问候
I solved my problem. 我解决了我的问题。 It was with the cursor connection.
它与光标连接有关。 In my view, I add :
我认为,我添加:
from django.db import connections
#cursor = connection.cursor()
cursor = connections['osm'].cursor()
Now, the connection know the database. 现在,连接知道数据库了。 My router is useless in this case.
我的路由器在这种情况下没有用。
Source : https://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly 来源: https : //docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-direct
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.