问题描述
我有一个项目,结构如下:
I have a project, structured like this:
project/
__init__.py
db/
models/
__init__.py
article.py
project.py
ontology/
__init__.py
coded.py
这是一个更大的一点,但这是想法。包含
It's a little bit bigger, but that's the idea. models.__init__.py
contains:
from db.models.article import *
from db.models.project import *
from db.models.ontology.coded import *
当运行syncdb时,它将忽略在模型中导入的所有模型.__ init __。py
。没有 ImportError
的,并且当向 __ init __。py
添加打印语句时,它很乐意打印导入模型(同时运行syncdb)。
When running syncdb, it ignores all models imported in models.__init__.py
. There are no ImportError
's, and when adding a print statement to the __init__.py
, it happily prints the import models (while running syncdb).
中定义的模型 __ init __。py
。
Models defined in __init__.py
work though.
为什么?可以强制syncdb来计算导入的模型吗?
Why is that? Can I force syncdb to account for my imported models?
编辑:INSTALLED_APPS中的应用程序 :
The application is in INSTALLED_APPS:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'amcatnavigator.navigator',
'amcatnavigator.db',
)
谢谢!
推荐答案
(syncdb)docs:它将为在settings.py文件中INSTALLED_APPS部分的那些模型创建表。如果模型被使用,但是它已经改变了,你不想丢失任何数据 - 使用迁移:
According to South (syncdb) docs: http://south.aeracode.org/docs/tutorial/part1.html It will create tables only for those models that are in INSTALLED_APPS section in settings.py file. If model is being used, but its changed and you don't want to lose any data - use migrations: http://south.aeracode.org/docs/tutorial/part1.html#the-first-migration
更新:至于我通过设计看Django,不会在不同的目录中找到模型:你可能想使用app_label
UPDATE: As far as i looked Django by design wont find the models within different directories: http://code.djangoproject.com/ticket/14007 you might want to use app_label
更新:app_label docs:
UPDATE: app_label docs: http://docs.djangoproject.com/en/dev/ref/models/options/#app-label
这篇关于syncdb忽略导入的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!