Commit bfcc686d authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

Removed the only_with_models_module argument of get_model[s].

Now that the refactorings are complete, it isn't particularly useful any
more, nor very well named. Let's keep the API as simple as possible.

Fixed #21689.
parent ec020cab
Loading
Loading
Loading
Loading
+14 −21
Original line number Diff line number Diff line
@@ -101,35 +101,24 @@ class Apps(object):
                "App registry isn't populated yet. "
                "Have you called django.setup()?")

    def get_app_configs(self, only_with_models_module=False):
    def get_app_configs(self):
        """
        Imports applications and returns an iterable of app configs.

        If only_with_models_module in True (non-default), imports models and
        considers only applications containing a models module.
        """
        self.check_ready()
        for app_config in self.app_configs.values():
            if only_with_models_module and app_config.models_module is None:
                continue
            yield app_config
        return self.app_configs.values()

    def get_app_config(self, app_label, only_with_models_module=False):
    def get_app_config(self, app_label):
        """
        Imports applications and returns an app config for the given label.

        Raises LookupError if no application exists with this label.

        If only_with_models_module in True (non-default), imports models and
        considers only applications containing a models module.
        """
        self.check_ready()
        app_config = self.app_configs.get(app_label)
        if app_config is None:
        try:
            return self.app_configs[app_label]
        except KeyError:
            raise LookupError("No installed app with label '%s'." % app_label)
        if only_with_models_module and app_config.models_module is None:
            raise LookupError("App '%s' doesn't have a models module." % app_label)
        return app_config

    # This method is performance-critical at least for Django's test suite.
    @lru_cache.lru_cache(maxsize=None)
@@ -319,11 +308,14 @@ class Apps(object):
            "get_app_config(app_label).models_module supersedes get_app(app_label).",
            PendingDeprecationWarning, stacklevel=2)
        try:
            return self.get_app_config(
                app_label, only_with_models_module=True).models_module
            models_module = self.get_app_config(app_label).models_module
        except LookupError as exc:
            # Change the exception type for backwards compatibility.
            raise ImproperlyConfigured(*exc.args)
        if models_module is None:
            raise ImproperlyConfigured(
                "App '%s' doesn't have a models module." % app_label)
        return models_module

    def get_apps(self):
        """
@@ -332,8 +324,9 @@ class Apps(object):
        warnings.warn(
            "[a.models_module for a in get_app_configs()] supersedes get_apps().",
            PendingDeprecationWarning, stacklevel=2)
        app_configs = self.get_app_configs(only_with_models_module=True)
        return [app_config.models_module for app_config in app_configs]
        app_configs = self.get_app_configs()
        return [app_config.models_module for app_config in app_configs
                if app_config.models_module is not None]

    def _get_app_package(self, app):
        return '.'.join(app.__name__.split('.')[:-1])
+1 −1
Original line number Diff line number Diff line
@@ -88,7 +88,7 @@ If you're unsure, answer 'no'.


def update_all_contenttypes(**kwargs):
    for app_config in apps.get_app_configs(only_with_models_module=True):
    for app_config in apps.get_app_configs():
        update_contenttypes(app_config, **kwargs)


+0 −3
Original line number Diff line number Diff line
@@ -344,9 +344,6 @@ class AppCommand(BaseCommand):
        from django.apps import apps
        if not app_labels:
            raise CommandError("Enter at least one application label.")
        # Don't use only_with_models_module=True in get_app_config() to tell
        # apart missing apps from apps without a model module -- which can't
        # be supported with the legacy API since it passes the models module.
        try:
            app_configs = [apps.get_app_config(app_label) for app_label in app_labels]
        except (LookupError, ImportError) as e:
+2 −2
Original line number Diff line number Diff line
@@ -82,8 +82,8 @@ class Command(BaseCommand):
            if primary_keys:
                raise CommandError("You can only use --pks option with one model")
            app_list = OrderedDict((app_config, None)
                for app_config in apps.get_app_configs(only_with_models_module=True)
                if app_config not in excluded_apps)
                for app_config in apps.get_app_configs()
                if app_config.models_module is not None and app_config not in excluded_apps)
        else:
            if len(app_labels) > 1 and primary_keys:
                raise CommandError("You can only use --pks option with one model")
+1 −1
Original line number Diff line number Diff line
@@ -93,6 +93,6 @@ Are you sure you want to do this?
        # Emit the post migrate signal. This allows individual applications to
        # respond as if the database had been migrated from scratch.
        all_models = []
        for app_config in apps.get_app_configs(only_with_models_module=True):
        for app_config in apps.get_app_configs():
            all_models.extend(router.get_migratable_models(app_config, database, include_auto_created=True))
        emit_post_migrate_signal(set(all_models), verbosity, interactive, database)
Loading