简体   繁体   English

强制Django在同一应用中识别多个“模型”文件夹?

[英]Force Django to recognize multiple “models” folders in the same app?

My Django project is laid out strangely: 我的Django项目的布局很奇怪:

/project
    |----manage.py
    |----/project
        |----settings.py
        |----urls.py
    |----/main
        |----/users
            |----/models
                |----__init__.py
                |----model1.py
                |----model2.py
                |----model3.py
            |----/views
                |----__init__.py
                |----view1.py
                |----view2.py
            |----/forms
                |----__init__.py
                |----form1.py
                |---form2.py
            |----urls.py
        |----/activities
            |----/models
                |----__init__.py
                |----model1.py
                |----model3.py
            |----/views
                |----__init__.py
                |----view1.py
                |----view2.py
            |----/forms
                |----__init__.py
                |----form1.py
            |----urls.py
        |----/migrations
        |----urls.py
        |----admin.py
        |----apps.py

This is almost a normal setup—except users and activities aren't technically apps.; 几乎是正常的设置-除了usersactivities从技术上来说不是应用程序。 main is the app. main是应用程序。 My goal is to keep all migrations linear. 我的目标是使所有迁移保持线性。

When I run python manage.py , Django sees the models in main/users/models but not in main/activities/models . 当我运行python manage.py ,Django在main/users/models看到模型,但在main/activities/models python manage.py不到main/activities/models

Is there a way of forcing Django to look in both folders when considering its migrations. 在考虑其迁移时,是否有一种强迫Django在两个文件夹中查找的方法。 I could create models folder w/ __init__.py that imports everything in main , but, if possible, I'd like to keep my current setup and just change where Django looks. 我可以创建带有__init__.py models文件夹,将其导入main所有内容,但是,如果可能的话,我想保留当前设置并仅更改Django的外观。

There are two problems you'll need to overcome in this case. 在这种情况下,您需要解决两个问题。

  1. There is no setting to get Django to look for modules in a module other than models . 没有设置让Django在models之外的模块中寻找模块。 In fact, it's a hard-coded module name. 实际上,这是一个硬编码的模块名称。

  2. Even if you could override the module that Django it looking for, it's still only looking in a single module, and you have two. 即使您可以覆盖Django查找的模块,它仍然只在一个模块中查找,而您只有两个模块。

You have a few options to make this work. 您可以选择几种方法来完成这项工作。 First, I would strongly consider breaking this one app into two. 首先,我会强烈考虑将这个应用程序分成两部分。 You say that users and activities aren't apps, but their structures scream otherwise: not only do they each have their own models , they have their own views , forms , and urls . 您说usersactivities不是应用程序,但是它们的结构却在尖叫:它们不仅具有各自的models ,而且具有各自的viewsformsurls You'll end up with a much better structure if you make them their own, full-fledged apps. 如果将它们制作为自己的成熟应用程序,则会得到更好的结构。

If you'd really rather keep the existing structure, then you have two options. 如果您确实希望保留现有结构,则有两个选择。 The easiest is to simply create a main/models.py and import your respective models from there. 最简单的方法是简单地创建一个main/models.py并从那里导入您各自的模型。

main/models.py main / models.py

from .users.models import *
from .activities.models import *

Simple as that. 就那么简单。 The more complicated option is to override the .get_models() and .get_model() methods in your AppConfig class in main/apps.py . 更复杂的选项是在main/apps.py中的AppConfig类中重写.get_models().get_model()方法。 I can come up with an exmaple of this if you want to go this route. 如果您想走这条路线,我可以举一个例子。

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

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