Load application configurations and models. Import each application module and then each model module. It is thread-safe and idempotent, but not reentrant.
(self, installed_apps=None)
| 59 | self.populate(installed_apps) |
| 60 | |
| 61 | def populate(self, installed_apps=None): |
| 62 | """ |
| 63 | Load application configurations and models. |
| 64 | |
| 65 | Import each application module and then each model module. |
| 66 | |
| 67 | It is thread-safe and idempotent, but not reentrant. |
| 68 | """ |
| 69 | if self.ready: |
| 70 | return |
| 71 | |
| 72 | # populate() might be called by two threads in parallel on servers |
| 73 | # that create threads before initializing the WSGI callable. |
| 74 | with self._lock: |
| 75 | if self.ready: |
| 76 | return |
| 77 | |
| 78 | # An RLock prevents other threads from entering this section. The |
| 79 | # compare and set operation below is atomic. |
| 80 | if self.loading: |
| 81 | # Prevent reentrant calls to avoid running AppConfig.ready() |
| 82 | # methods twice. |
| 83 | raise RuntimeError("populate() isn't reentrant") |
| 84 | self.loading = True |
| 85 | |
| 86 | # Phase 1: initialize app configs and import app modules. |
| 87 | for entry in installed_apps: |
| 88 | if isinstance(entry, AppConfig): |
| 89 | app_config = entry |
| 90 | else: |
| 91 | app_config = AppConfig.create(entry) |
| 92 | if app_config.label in self.app_configs: |
| 93 | raise ImproperlyConfigured( |
| 94 | "Application labels aren't unique, " |
| 95 | "duplicates: %s" % app_config.label |
| 96 | ) |
| 97 | |
| 98 | self.app_configs[app_config.label] = app_config |
| 99 | app_config.apps = self |
| 100 | |
| 101 | # Check for duplicate app names. |
| 102 | counts = Counter( |
| 103 | app_config.name for app_config in self.app_configs.values() |
| 104 | ) |
| 105 | duplicates = [name for name, count in counts.most_common() if count > 1] |
| 106 | if duplicates: |
| 107 | raise ImproperlyConfigured( |
| 108 | "Application names aren't unique, " |
| 109 | "duplicates: %s" % ", ".join(duplicates) |
| 110 | ) |
| 111 | |
| 112 | self.apps_ready = True |
| 113 | |
| 114 | # Phase 2: import models modules. |
| 115 | for app_config in self.app_configs.values(): |
| 116 | app_config.import_models() |
| 117 | |
| 118 | self.clear_cache() |
no test coverage detected