通过 app_label 在子目录中模型?
为了将我的模型放在子文件夹中,我尝试使用 app_label 元字段,如所述 此处。
我的目录结构如下所示:
- 项目
- 应用程序
- 富
- 型号
- __init__.py
- bar_model.py
- 型号
- 富
- 应用程序
在 bar_model.py 中,我像这样定义我的模型:
from django.db import models
class SomeModel(models.Model):
field = models.TextField()
class Meta:
app_label = "foo"
我可以像这样成功导入模型:
from apps.foo.models.bar_model import SomeModel
但是,运行:
./manage.py syncdb
不会为模型创建表。然而,在详细模式下,我确实看到应用程序“foo”被正确识别(它位于 settings.py 中的 INSTALLED_APPS 中)。将模型移动到 foo 下的 models.py 确实有效。
app_label 或整个机制中是否有一些特定约定未记录,导致该模型结构无法被syncdb 识别?
In order to place my models in sub-folders I tried to use the app_label Meta field as described here.
My directory structure looks like this:
- project
- apps
- foo
- models
- __init__.py
- bar_model.py
- models
- foo
- apps
In bar_model.py I define my Model like this:
from django.db import models
class SomeModel(models.Model):
field = models.TextField()
class Meta:
app_label = "foo"
I can successfully import the model like so:
from apps.foo.models.bar_model import SomeModel
However, running:
./manage.py syncdb
does not create the table for the model. In verbose mode I do see, however, that the app "foo" is properly recognized (it's in INSTALLED_APPS in settings.py). Moving the model to models.py under foo does work.
Is there some specific convention not documented with app_label or with the whole mechanism that prevents this model structure from being recognized by syncdb?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅 Django 票证 #10985:解释如何在目录中组织模型
您可能是没有将模型导入到 __init__.py 中?
See Django ticket #10985: Explain how models can be organised in a directory
It may be that you aren't importing your models into
__init__.py
?syncdb
不会为不在.models
中的模型创建表,因此将其导入其中,例如from apps.foo.models import SomeModel< /代码>。
syncdb
will not create tables for models not located in<appname>.models
, so import it in there, e.g.from apps.foo.models import SomeModel
.如果您有较新版本的 Django,这里有一个解决方案,假设您有一个名为
subfolder
的子文件夹:在您的文件夹 app 的
apps.py
中:Here is a solution if you have a newer version of Django, supposing you have a subfolder named
subfolder
:in
apps.py
of your folder app: