Django's Apps Source learning

Source: Internet
Author: User

Learn about the official documentation

Django contains a registry of installed apps that store configuration information and a list of available models for introspection.

This registry is an application, located under Django.apps, essentially an instance of an apps class under the Django.apps.registry module.

See the truth before Source:

  

Class Apps (object):

This is a registry that stores configuration information for installed apps and also tracks models, such as providing reverse-relations.

Primary properties of initialization:

Apps_configs: This is an ordered dictionary that maps the label label of the AppConfig instance to an already installed entry (AppConfig).

Main methods:

Populate (Installded_app): Loads the configuration information and model of the application, this method imports each application module and then each model module, which is thread-safe and idempotent (the impact of any number of executions is the same as the impact of a single execution), But can not re-enter (reentrant), simple Baidu, should be unable to make recursive call meaning.

  

 ifSelf.ready: #该注册表是否已经被填充return        #populate () might is called by and threads in parallel on servers        #That create threads before initializing the WSGI callable.
# populate () can be called by two parallel threads on the server that will create the thread before the WSGI call is initialized. With Self._lock:
The #_lock () is a theading.lock () object that automatically acquires locks in the with context manager, after processing, and automatically releasing locks. ifSelf.ready:return #App_config should be pristine, otherwise the code below won ' t #Guarantee the order matches the order in Installed_apps.
# App_config should be in the original state, otherwise the following code will not guarantee that this order matches the order in Installed_apps.
ifSelf.app_configs:RaiseRuntimeError ("populate () isn ' t reentrant") #Phase 1:initialize app configs and import app modules. forEntryinchInstalled_apps:
# iterate every item that has installed appsifisinstance (Entry, AppConfig): App_config=entryElse:
# If the Installed_apps is not configured with the Python path of the AppConfig class but the app module path, it will be created in Factory mode. App_config=appconfig.create (Entry)ifApp_config.labelinchSelf.app_configs:
# detects the uniqueness of the app_config. Raiseimproperlyconfigured ("application labels aren ' t unique," "Duplicates:%s"%App_config.label) Self.app_configs[app_config.label]=the App_config # App_config.label property is obtained by default by App_name. App_config.apps=Self #将注册表赋给AppConfig类的apps属性. #Check for duplicate app names.Counts =Counter (App_config.name forApp_configinchself.app_configs.values ()) Duplicates=[Name forName, CountinchCounts.most_common ()ifCount > 1] ifDuplicates:Raiseimproperlyconfigured ("application names aren ' t unique," "Duplicates:%s"%", ". Join (Duplicates)) Self.apps_ready=True#Phase 2:import models modules.
# import Model Module
forApp_configinchself.app_configs.values (): App_config.import_models () Self.clear_cache () self. Models_ready=True#Phase 3:run Ready () Methods of app Configs.
# Run the Ready () method of the apps configs.
forApp_configinchself.get_app_configs (): App_config.ready () Self.ready= True

Django's Apps Source learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.