1. 程式人生 > 實用技巧 >django APP註冊 pupulate函式

django APP註冊 pupulate函式

 1     def populate(self, installed_apps=None):
 2         """
 3         Loads application configurations and models.
 4 
 5         This method imports each application module and then each model module.
 6 
 7         It is thread safe and idempotent, but not reentrant.
 8         """
 9         if
self.ready: 10 return 11 12 # populate() might be called by two threads in parallel on servers 13 # that create threads before initializing the WSGI callable. 14 with self._lock: 15 if self.ready: 16 return 17 18 # app_config should be pristine, otherwise the code below won't
19 # guarantee that the order matches the order in INSTALLED_APPS. 20 if self.app_configs: 21 raise RuntimeError("populate() isn't reentrant") 22 23 # Phase 1: initialize app configs and import app modules. 24 for entry in installed_apps:
25 if isinstance(entry, AppConfig): 26 app_config = entry 27 else: 28 app_config = AppConfig.create(entry) 29 if app_config.label in self.app_configs: 30 raise ImproperlyConfigured( 31 "Application labels aren't unique, " 32 "duplicates: %s" % app_config.label) 33 34 self.app_configs[app_config.label] = app_config 35 app_config.apps = self 36 37 # Check for duplicate app names. 38 counts = Counter( 39 app_config.name for app_config in self.app_configs.values()) 40 duplicates = [ 41 name for name, count in counts.most_common() if count > 1] 42 if duplicates: 43 raise ImproperlyConfigured( 44 "Application names aren't unique, " 45 "duplicates: %s" % ", ".join(duplicates)) 46 47 self.apps_ready = True 48 49 # Phase 2: import models modules. 50 for app_config in self.app_configs.values(): 51 app_config.import_models() 52 53 self.clear_cache() 54 55 self.models_ready = True 56 57 # Phase 3: run ready() methods of app configs. 58 for app_config in self.get_app_configs(): #原始碼無try 59 try: 60 app_config.ready() 61 except: 62 pass 63 64 self.ready = True

celery任務啟動時,admin註冊出錯,需做修改。