我对django相当陌生,我正在用它来制作一个网络游戏的网站。这个游戏已经有了自己的东西,所以我用它作为django的定制auth模型。
我创建了一个名为“帐户”的新应用程序,把这些东西放进去,并添加了模型。我添加了路由器并在设置中启用了它,一切都很好,我可以从管理站点登录并做一些事情。
现在我也在努力学习TDD,所以我需要将auth数据库转储到一个固定设备上。当我运行./manage.py dumpdata account时,我会得到一个空数组。没有任何错误,也没有任何回溯,只是一个空数组。我已尽我所能地摆弄它,但我似乎找不到问题所在。
以下是一些相关的设置。
数据库
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'censored',
'USER': 'censored',
'PASSWORD': 'censored',
'HOST': 'localhost',
'PORT': '',
},
'auth_db': {
'ENGINE': 'mysql_pymysql',
'NAME': 'censored',
'USER': 'censored',
'PASSWORD': 'censored',
'HOST': '127.0.0.1',
'PORT': '3306'
}
}路由器
class AccountRouter(object):
"""
A router to control all database operations on models in the
account application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read account models go to auth_db.
"""
if model._meta.app_label == 'account':
return 'auth_db'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write account models go to auth_db.
"""
if model._meta.app_label == 'account':
return 'auth_db'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the account app is involved.
"""
if obj1._meta.app_label == 'account' or \
obj2._meta.app_label == 'account':
return True
return None
def allow_syncdb(self, db, model):
"""
Make sure the account app only appears in the 'auth_db'
database.
"""
if model._meta.app_label == 'account':
return False
return NoneDjango设置
DATABASE_ROUTERS = ['account.router.AccountRouter']我真的不知道该尝试什么,任何帮助或想法都很感激。
发布于 2013-11-05 13:04:11
我也有同样的问题,你需要指定正确的数据库。例如,给定您的代码:
$ ./manage.py dumpdata --database=auth_db account发布于 2017-10-03 08:56:27
我也有类似的问题。创建一个名为models.py的空文件为我解决了这个问题。检查您的应用程序目录中是否有这样的文件,如果没有-创建一个。
发布于 2015-04-22 10:33:12
./manage.py dumpdata命令将在运行时保持沉默,并输出[]。因此,建议是在./manage.py shell中运行模型的代码,并且存在目标数据,例如: `from account.models import Account print Account.objects.all()[:1]` ./manage.py dumpdata能够找到targe模型。Django通过{APP_NAME}.models查找模型,如果您将模型放在目录account/models/中,则在account/models/__init__.py中导入模型,例如:from profile import Profilehttps://stackoverflow.com/questions/18217385
复制相似问题