我已经将这个问题拆开了大约一个星期。我查看了所有其他stackoverflow帖子,但无法弄明白。无论如何,我试图在我的项目中添加geodjango应用程序。这个项目还在开发中,无论如何,我有两个数据库,默认和一个标记为locations_db。我想在location数据库中使用geodjango功能,在默认数据库中需要所有其他内容。位置数据库是一个可以正常工作的postGIS数据库,默认是一个mysql数据库。我使用postGIS shell测试了位置数据库,我开始了一个新的django项目并使其成为纯粹的geodjango。两者都很好。但是,当我尝试运行任何类型的manage.py命令时,它会抛出错误:' DatabaseOperations'对象没有属性' geo_db_type'。我尝试使用--database选项进行迁移并使用数据库路由器。我该如何解决这个问题?作为参考,这是我的settings.py摘录有关数据库:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'xxxx',
"USER":"xxxx",
"PASSWORD":"xxxxxxxxx",
'HOST': 'xxxxxxxxxx',
'PORT': 'xxxx',
},
'locations_db':{
'ENGINE':"django.contrib.gis.db.backends.postgis",
'NAME': 'xxxxxxxxxx',
"USER":"xxxxxxxxx",
"PASSWORD":"xxxxxxxxxxxxxxxx",
'HOST': 'xxxxxxxxx',
'PORT': 'xxxx',
}
}
...
DATABASE_ROUTERS = ['locations.router.Router']
DATABASES['locations_db']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'
这是我的路由器:
class Router(object):
def db_for_read(self, model, **hints):
"Point all operations on locations models to 'locationsdb'"
if model._meta.app_label == 'locations':
return 'locations_db'
return None
def db_for_write(self, model, **hints):
"Point all operations on locations models to 'locationsdb'"
# print("working not in locations?")
if model._meta.app_label == 'locations':
print(model._meta.app_label)
return 'locations_db'
return None
def allow_relation(self, obj1, obj2, **hints):
"Allow any relation if a both models in locations app"
# if obj1._meta.app_label == 'locations' and obj2._meta.app_label == 'locations':
# return True
# # Allow if neither is locations app
# elif 'locations' not in [obj1._meta.app_label, obj2._meta.app_label]:
# return True
if(obj2._meta.app_label == 'locations' or obj1._meta.app_label == "locations"):
return True
return None
def allow_migrate(self, db, model):
if(db == "locations_db"):
return model._meta.app_label == 'locations'
elif model._meta.app_label == 'locations':
return False
else:
return None
def allow_syncdb(self, db, model):
# print("working not in locations?")
if db == 'locations_db' or model._meta.app_label == "locations":
print(model._meta.app_label)
return False # we're not using syncdb on our legacy database
else: # but all other models/databases are fine
return True
和我的模特:
class Locations(models.Model):
name = models.CharField(max_length=254)
area = models.IntegerField()
pop2005 = models.IntegerField('Population 2005')
fips = models.CharField('FIPS Code', max_length=2)
iso2 = models.CharField('2 Digit ISO', max_length=2)
iso3 = models.CharField('3 Digit ISO', max_length=3)
un = models.IntegerField('United Nations Code')
region = models.IntegerField('Region Code')
subregion = models.IntegerField('Sub-Region Code')
lon = models.FloatField()
lat = models.FloatField()
# GeoDjango-specific: a geometry field (MultiPolygonField), and
# overriding the default manager with a GeoManager instance.
mpoly = models.PolygonField()
objects = models.GeoManager()
提前感谢您的协助!
编辑:我正在使用django 1.8又称开发版。 postGIS版本2.1和postgres版本9.3