通过app_label在子目录中的模型? [英] Model in sub-directory via app_label?

查看:118
本文介绍了通过app_label在子目录中的模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了将我的模型放在子文件夹中,我尝试使用app_label Meta字段,如这里



我的目录结构如下所示:




  • 项目


    • apps


      • foo


        • 模型


          • __ 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 
/ pre>

但是,runn ing:

  ./ manage.py syncdb 

不会为模型创建表。在详细模式中,我确实看到应用程序foo被正确识别(它在settings.py中的INSTALLED_APPS中)。将模型移动到foo下的models.py是有效的。



有没有一些具体的约定没有记录在app_label或整个机制,阻止这个模型结构被识别syncdb?

解决方案

请参阅Django ticket #10985:解释如何在目录中组织模型



可能是您没有将模型导入 __ init __。py


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

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?

解决方案

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?

这篇关于通过app_label在子目录中的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆