简体   繁体   English

共享Django项目结构

[英]shared django project structure

This is what the structure of my project is. 这就是我项目的结构。 This question will ignore the 'lib' directory: 这个问题将忽略'lib'目录:

project
    app
        data -> migrations, models, __init__.py
        prodsite -> settings.py, urls.py, wsgi.py, __init__.py
        testsite -> same as above
    lib
        data -> migrations, models, __init__.py
        prodsite -> settings.py, urls.py, wsgi.py, __init__.py, common models
        testsite -> same as above
    tools
        manage.py

I want to be able to run: 我希望能够运行:

python tools/manage.py app.prodsite (makemigrations/migrate) app

It was not hard to modify manage.py so that it adds the root directory to the system path, sets the django environment variable to 'app.prodsite', and deletes that argument from sys.argv, before continuing on with what manage.py normally does. 修改manage.py以便在系统目录中添加根目录,将django环境变量设置为'app.prodsite'并从sys.argv中删除该参数,然后继续进行manage.py并不难。通常会。 I could not manage to get rid of the last parameter without causing errors: 我无法摆脱最后一个参数而不会导致错误:

#!/usr/bin/env python
import os
import sys
import inspect

def main():
    sys.path.append(os.getcwd()) # allow reative to cwd, not just module.py.
    if "DJANGO_SETTINGS_MODULE" not in os.environ:
        if len(sys.argv) < 3:
            print("Error in command: {}".format(" ".join(sys.argv)))
            print("manage.py should be called like:\r\n"
                  "manage.py <app> <operation>.")
            return
        os.environ.setdefault("DJANGO_SETTINGS_MODULE",
                              "{}.settings".format(sys.argv[1]))
        del sys.argv[1]
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

if __name__ == "__main__":
    main()

These are the other relevant files: 这些是其他相关文件:

# project.app.data:
class M(Model):
    class Meta:
        app_label = 'app.data'
    a: bool
    b: str
    c: float

# project.app.data.apps
class DataConfig(AppConfig):
    name = 'app.data'

# project.app.data.__init__
default_app_config = 'app.data.apps.DataConfig'

# project.app.prodsite.settings
ROOT_URLCONF = 'app.prodsite.urls'
WSGI_APPLICATION = 'app.prodsite.wsgi.application'
INSTALLED_APPS += ['app.data', 'app.micsite']

If I try running 'python bldtool/manage.py app.prodsite makemigrations app.data', I get an error asking if app.data is in INSTALLED_APPS. 如果我尝试运行“ python bldtool / manage.py app.prodsite makemigrations app.data”,则会出现错误,询问app.data是否在INSTALLED_APPS中。

No errors are generated if I only include 'app' in INSTALLED_APPS, but then django does not detect any changes and does not generate a migrations directory. 如果我仅在INSTALLED_APPS中包含“ app”,则不会生成任何错误,但是django不会检测到任何更改,也不会生成迁移目录。 (I have tried creating a migrations directory with init .py, with no success, either) (我尝试用init .py创建迁移目录,也没有成功)

What can I do to make this work as expected? 我怎样做才能使这项工作按预期进行?

According to django documentations: 根据Django文档:

A list of strings designating all applications that are enabled in this Django installation. 指定此Django安装中启用的所有应用程序的字符串列表。 Each string should be a dotted Python path to: 每个字符串都应是指向以下位置的Python虚线路径:

  • an application configuration class (preferred), or 应用程序配置类(首选),或者
  • a package containing an application. 包含应用程序的软件包。

Also, migrations directories ( with __init__.py files inside them) should be created before running makemigrations without specifying app name, otherwise, no changes will be detected by makemigrations command. 另外,应在运行makemigrations之前创建迁移目录(其中包含__init__.py文件),而不指定应用程序名称,否则,makemigrations命令将不会检测到任何更改。 But if you specify app name, makemigrations will work even without migrations directory. 但是,如果您指定应用程序名称,即使没有迁移目录,makemigrations也可以使用。

The configuration that works for me in this particular case is: 在这种特殊情况下适合我的配置是:

# project.app.data:
class M(Model):
    class Meta:
        app_label = 'data' # Not full path
    a: bool
    b: str
    c: float

# project.app.data.apps
class DataConfig(AppConfig):
    name = 'app.data'

# project.app.data.__init__
# Empty

# project.app.prodsite.settings
ROOT_URLCONF = 'app.prodsite.urls'
WSGI_APPLICATION = 'app.prodsite.wsgi.application'
INSTALLED_APPS += ['app.data', 'app'] # No need for app.micsite

The above (along with the modified manage.py) allows me to perform: 上面的代码(以及修改后的manage.py)允许我执行:

python tools/manage.py app.prodsite [makemigrations|migrate] data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM