Python django.apps 模块,AppConfig() 实例源码

我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用django.apps.AppConfig()

项目:morango    作者:learningequality    | 项目源码 | 文件源码
def ready(self):
        from django.core.management import call_command
        from morango.models import InstanceIDModel
        from morango.certificates import ScopeDefinition
        from .signals import add_to_deleted_models  # noqa: F401

        # NOTE: Warning: https://docs.djangoproject.com/en/1.10/ref/applications/#django.apps.AppConfig.ready
        # its recommended not to execute queries in this method, but we are producing the same result after the first call, so its OK

        # call this on app load up to get most recent system config settings
        try:
            InstanceIDModel.get_or_create_current_instance()
            if not ScopeDefinition.objects.filter():
                call_command("loaddata", "scopedefinitions")
        # we catch this error in case the database has not been migrated, b/c we can't query it until its been created
        except (OperationalError, ProgrammingError):
            pass

        # add models to be synced by profile
        add_syncable_models()
项目:django-north    作者:peopledoc    | 项目源码 | 文件源码
def test_get_all_contenttypes_for_app_config(mocker):
    model1 = mocker.Mock(_meta=mocker.Mock(model_name='mymodel1'))
    model2 = mocker.Mock(_meta=mocker.Mock(model_name='mymodel2'))
    ct1 = ContentType(app_label='myapp', model='mymodel1')
    ct2 = ContentType(app_label='myapp', model='mymodel2')
    app_config = CustomAppConfig('myapp', mocker.Mock())

    mocker.patch(
        'django.apps.AppConfig.get_models', return_value=[model1, model2])
    mock_get_for_model = mocker.patch(
        'django.contrib.contenttypes.models.ContentTypeManager.get')

    mock_get_for_model.side_effect = [ct1, ct2]
    result = permissions.get_all_contenttypes_for_app_config(app_config)

    assert result == [(ct1, model1), (ct2, model2)]

    mock_get_for_model.side_effect = [ct1, ContentType.DoesNotExist]
    result = permissions.get_all_contenttypes_for_app_config(app_config)

    assert result == [(ct1, model1)]
项目:django-postgres-extra    作者:SectorLabs    | 项目源码 | 文件源码
def __init__(self):
        """Creates a new migration simulator with an empty
        project state and no migrations."""

        import psqlextra.apps

        self.app_label = self._generate_random_name()
        self.app_config = type(self.app_label, (AppConfig,), dict(
            name=self.app_label,
            verbose_name=self.app_label
        ))(self.app_label, psqlextra.apps)

        self.app_config.models = {}

        self.apps = Apps()
        self.apps.ready = False
        self.apps.populate(installed_apps=[self.app_config])

        self.project_state = ProjectState()
        self.migrations = []
项目:fakester    作者:pawelad    | 项目源码 | 文件源码
def test_redirects_app_config():
    """Test 'redirects' module `AppConfig` instance"""
    redirects_app_config = fakester_apps.get_app_config('redirects')

    assert isinstance(redirects_app_config, AppConfig)
    assert redirects_app_config.name == 'redirects'
    assert redirects_app_config.verbose_name == 'redirects'
项目:ecs    作者:ecs-org    | 项目源码 | 文件源码
def ready(self):
        # XXX: should be imported in the AppConfig of the respective app itself
        import ecs.core.triggers
        import ecs.votes.triggers
        import ecs.meetings.triggers

        # Patch the user __str__ method, so the hash in the username field
        # does not show up.
        # XXX: We should be using a custom user model instead.
        from django.contrib.auth.models import User
        from ecs.users.utils import get_full_name
        User.__str__ = get_full_name
项目:cyphon    作者:dunbarcyber    | 项目源码 | 文件源码
def ready(self):
        """Override the default :meth:`~django.apps.AppConfig.ready` method.

        Registers :mod:`~tags.signals` used in the app.
        """
        import tags.signals  # noqa: F401
项目:cyphon    作者:dunbarcyber    | 项目源码 | 文件源码
def ready(self):
        """Override the default :meth:`~django.apps.AppConfig.ready` method.

        Registers :mod:`~distilleries.signals` used in the app.
        """
        import distilleries.signals  # noqa: F401
项目:cyphon    作者:dunbarcyber    | 项目源码 | 文件源码
def ready(self):
        """Override the default :meth:`~django.apps.AppConfig.ready` method.

        Registers :mod:`~warehouses.signals` used in the app.
        """
        import warehouses.signals  # noqa: F401
项目:django-north    作者:peopledoc    | 项目源码 | 文件源码
def test_get_known_models_for_app_config(mocker):
    model1 = mocker.Mock(_meta=mocker.Mock(model_name='mymodel1'))
    model2 = mocker.Mock(_meta=mocker.Mock(model_name='mymodel2'))
    app_config = CustomAppConfig('myapp', mocker.Mock())

    mocker.patch(
        'django.apps.AppConfig.get_models', return_value=[model1, model2])

    result = contenttypes.get_known_models_for_app_config(app_config)

    assert result == {
        'mymodel1': model1,
        'mymodel2': model2,
    }
项目:djangolg    作者:wolcomm    | 项目源码 | 文件源码
def test_app_import(self):
        """Test importing djangolg app."""
        from djangolg import apps
        assert issubclass(apps.DjangolgConfig, AppConfig)
        assert apps.DjangolgConfig.name == "djangolg"