我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用django.apps.apps.ready()。
def mock_django_setup(settings_module, disabled_features=None): """ Must be called *AT IMPORT TIME* to pretend that Django is set up. This is useful for running tests without using the Django test runner. This must be called before any Django models are imported, or they will complain. Call this from a module in the calling project at import time, then be sure to import that module at the start of all mock test modules. Another option is to call it from the test package's init file, so it runs before all the test modules are imported. :param settings_module: the module name of the Django settings file, like 'myapp.settings' :param disabled_features: a list of strings that should be marked as *False* on the connection features list. All others will default to True. """ if apps.ready: # We're running in a real Django unit test, don't do anything. return if 'DJANGO_SETTINGS_MODULE' not in os.environ: os.environ['DJANGO_SETTINGS_MODULE'] = settings_module django.setup() mock_django_connection(disabled_features)
def register(cls): """ Decorator function that registers a class to record diffs. @diffs.register class ExampleModel(models.Model): ... """ from django.apps import apps as django_apps from dirtyfields import DirtyFieldsMixin from .models import DiffModelManager, DiffModelDescriptor from .signals import connect # check if class implemented get_dirty_fields else hack in dirtyfields if not hasattr(cls, 'get_dirty_fields') and DirtyFieldsMixin not in cls.__bases__: cls.__bases__ = (DirtyFieldsMixin,) + cls.__bases__ setattr(cls, 'diffs', DiffModelDescriptor(DiffModelManager(cls))) if not django_apps.ready: klasses_to_connect.append(cls) else: connect(cls) return cls
def reinitialize_django_apps(self): apps.app_configs = OrderedDict() # set ready to false so that populate will work apps.ready = False # re-initialize them all; is there a way to add just one without reloading them all? apps.populate(settings.INSTALLED_APPS)
def model_unpickle(model_id, attrs, factory): """ Used to unpickle Model subclasses with deferred fields. """ if isinstance(model_id, tuple): if not apps.ready: apps.populate(settings.INSTALLED_APPS) model = apps.get_model(*model_id) else: # Backwards compat - the model was cached directly in earlier versions. model = model_id cls = factory(model, attrs) return cls.__new__(cls)
def reinit_plugin(name, upmate: bool=False): # pragma: no cover """ Reinitialize gitmate with plugin and upmate, if specified. """ app_name = 'gitmate_' + name app_config_name = 'plugins.{}.apps.{}Config'.format( app_name, snake_case_to_camel_case(app_name)) if app_config_name in settings.INSTALLED_APPS: return settings.GITMATE_PLUGINS += [name] settings.INSTALLED_APPS += [app_config_name] # To load the new app let's reset app_configs, the dictionary # with the configuration of loaded apps apps.app_configs = OrderedDict() # set ready to false so that populate will work apps.ready = False # re-initialize them all apps.populate(settings.INSTALLED_APPS) # migrate the models management.call_command('migrate', app_name, interactive=False) # upmate the plugins, if specified if upmate is True: management.call_command('upmate', interactive=False)
def find_setting(group, key, site=None): """Get a setting or longsetting by group and key, cache and return it.""" siteid = _safe_get_siteid(site) setting = None use_db, overrides = get_overrides(siteid) ck = cache_key('Setting', siteid, group, key) if use_db: try: setting = cache_get(ck) except NotCachedError as nce: if hasattr(apps, 'ready'): app_cache_ready = apps.ready else: app_cache_ready = apps.app_cache_ready() if app_cache_ready: try: setting = Setting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group) except Setting.DoesNotExist: # maybe it is a "long setting" try: setting = LongSetting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group) except LongSetting.DoesNotExist: pass cache_set(ck, value=setting) else: grp = overrides.get(group, None) if grp and key in grp: val = grp[key] setting = ImmutableSetting(key=key, group=group, value=val) log.debug('Returning overridden: %s', setting) if not setting: raise SettingNotSet(key, cachekey=ck) return setting