Django's process of finding model classes

In Django, the process of finding model classes

The relationship between models and applications in django

In Django, models must belong to an application. Django's models are organized and managed by applications, and each model must belong to an application.

A Django application is an independent functional unit that can contain multiple models, views, URL routing and other components to implement specific functions or business logic. Each application has its own directory structure that contains model definitions, view functions, template files, etc. related to that application.

While models must belong to an application, Django doesn't enforce that a project can have only one application. Depending on the size and needs of your project, you can create one or more apps to organize and manage your models.

The process of finding a model class

Django determines which applications contain model classes based on the INSTALLED_APPS list you set in your project's configuration file (settings.py). This list contains all the applications used in the Django project.

  1. When using model classes, Django first searches for the applications listed in INSTALLED_APPS. It looks for the models.py file under each application, which is where the model class definitions are placed by default.

  2. Django uses Python's built-in importlib package to load each application's models.py file and parse the code in it.

  3. During this process, Django creates a registry of all model classes, accessible through the django.apps.registry module. This registry maintains the mapping between application and model classes.

  4. When you reference a model class in your code, Django looks up the definition of that model class in the registry. If a matching model class is found, it can be used.

  5. Note that if multiple model classes are defined in an application's models.py file, they will all be registered and available for use.

By default, Django searches the top-level directory of each application and looks for a models.py file. It does not search subdirectories recursively. For example:

myapp/
├── models.py
└── subdirectory/
    ├── models.py
    └── other_models.py

By default, Django looks for model class definitions in each application's models.py file. If you don't define your model classes in your models.py file, Django won't be able to autoload them.

Django will only look for the models.py file in the myapp directory, and will not automatically load model classes in the models.py or other_models.py files in the subdirectory directory.

If you want Django to be able to load model classes in subdirectories, you can import them manually and register them in your application's models.py file. For example, in myapp/models.py:

from .subdirectory.models import MyModel

# 手动导入并注册子目录中的模型类

By manually importing model classes in subdirectories, you can extend Django's default model loading mechanism and enable it to load model class definitions from more locations.

The independent process uses the django model to report an error: jango.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

When you use Django models in a separate process, you may encounter django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. errors. This is because Django needs to load the application configuration and register the application during startup, and in a standalone process, Django has not yet done these things.

Make sure you have initialized the Django application configuration before using Django models. Django can be manually initialized by executing the following code:

import django
from django.conf import settings

# 设置 Django 配置模块
settings.configure(DEBUG=True, ...)  # 使用你的实际配置

# 调用 django.setup() 初始化 Django
django.setup()

This way, Django will initialize according to your configuration, including loading the application, registering models, etc.

Guess you like

Origin blog.csdn.net/inthat/article/details/131400463