我按照下面的doc形式django官方 https://docs.djangoproject.com/en/1.10/topics/db/multi-db/#automatic-database-routing
我尝试自动数据库路由。
我的设置就是那样
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'zipcode',
]
DATABASES = {
'default': {},
'blog': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'testdjango',
'USER': 'dany',
'PASSWORD': '****',
},
'zipcode': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'zipcode',
'USER': 'dany',
'PASSWORD': '****',
}
}
DATABASES_ROUTERS = ['blog.router.ZipcodeRouter']
class ZipcodeRouter(object):
def db_for_read(self, model, **hints):
if model._meta.app_label == 'zipcode':
return 'zipcode'
return 'blog'
def db_for_write(self, model, **hints):
if model._meta.app_label == 'zipcode':
return False
return 'blog'
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == 'zipcode' or obj2._meta.app_label == 'zipcode':
return False
return 'default'
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label == 'zipcode':
return False
return True
from django.db import models
from django.utils import timezone
class Post(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
class Gyeonggi(models.Model):
new_zipcode = models.CharField(max_length=20)
city = models.CharField(max_length=20)
...
old_zipcode = models.CharField(max_length=10, blank=True, null=True)
zipcode_code = models.CharField(max_length=30, blank=True, null=True)
class Meta:
managed = False
db_table = 'gyeonggi'
现在我尝试使用QuerySet表单python manage.py shell
from zipcode.models import Gyeonggi
Gyeonggi.objects.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\NPOST\.virtualenvs\django\lib\site-packages\django\db\models\query.py", line 232, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "C:\Users\NPOST\.virtualenvs\django\lib\site-packages\django\db\models\query.py", line 256, in __iter__
self._fetch_all()
File "C:\Users\NPOST\.virtualenvs\django\lib\site-packages\django\db\models\query.py", line 1087, in _fetch_all
self._result_cache = list(self.iterator())
File "C:\Users\NPOST\.virtualenvs\django\lib\site-packages\django\db\models\query.py", line 54, in __iter__
results = compiler.execute_sql()
File "C:\Users\NPOST\.virtualenvs\django\lib\site-packages\django\db\models\sql\compiler.py", line 824, in execute_sql
sql, params = self.as_sql()
File "C:\Users\NPOST\.virtualenvs\django\lib\site-packages\django\db\models\sql\compiler.py", line 369, in as_sql
extra_select, order_by, group_by = self.pre_sql_setup()
File "C:\Users\NPOST\.virtualenvs\django\lib\site-packages\django\db\models\sql\compiler.py", line 46, in pre_sql_setup
self.setup_query()
File "C:\Users\NPOST\.virtualenvs\django\lib\site-packages\django\db\models\sql\compiler.py", line 37, in setup_query
self.select, self.klass_info, self.annotation_col_map = self.get_select()
File "C:\Users\NPOST\.virtualenvs\django\lib\site-packages\django\db\models\sql\compiler.py", line 226, in get_select
ret.append((col, self.compile(col, select_format=True), alias))
File "C:\Users\NPOST\.virtualenvs\django\lib\site-packages\django\db\models\sql\compiler.py", line 353, in compile
sql, params = node.as_sql(self, self.connection)
File "C:\Users\NPOST\.virtualenvs\django\lib\site-packages\django\db\models\expressions.py", line 659, in as_sql
return "%s.%s" % (qn(self.alias), qn(self.target.column)), []
File "C:\Users\NPOST\.virtualenvs\django\lib\site-packages\django\db\models\sql\compiler.py", line 344, in quote_name_unless_alias
r = self.connection.ops.quote_name(name)
File "C:\Users\NPOST\.virtualenvs\django\lib\site-packages\django\db\backends\dummy\base.py", line 21, in complain
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: **settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentati
on for more details.**
运行QuerySet后出现此错误。
因此,我检查下一个。
Gyeonggi._meta.app_label
'zipcode'
我可以找到&#39; model._meta.app_label&#39;是&#39;邮政编码&#39;但是router.py没有工作。
我遗失了什么?
我不想使用(&#39; zipcode&#39;)。
我该如何解决?
感谢任何帮助!