Python django.apps.apps.all_models() Examples

The following are 17 code examples of django.apps.apps.all_models(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.apps.apps , or try the search function .
Example #1
Source File: models.py    From django-collaborative with MIT License 6 votes vote down vote up
def delete(self, **kwargs):
        # first drop the table, we have to do this first, else
        # django will complain about no content type existing
        Model = apps.get_model("django_models_from_csv", self.name)
        ModelSchemaEditor().drop_table(Model)

        # then remove django app and content-types/permissions
        app_config = apps.get_app_config("django_models_from_csv")
        wipe_models_and_permissions(app_config, self.name)

        # finally kill the row
        super().delete(**kwargs)

        # delete it from the django app registry
        try:
            del apps.all_models["django_models_from_csv"][self.name]
        except KeyError as err:
            raise LookupError("'{}' not found.".format(self.name))

        # Unregister the model from the admin, before we wipe it out
        try:
            admin.site.unregister(Model)
        except admin.sites.NotRegistered:
            pass 
Example #2
Source File: utils.py    From django-dynamic-models with MIT License 5 votes vote down vote up
def is_registered(self, model_name):
        return model_name.lower() in apps.all_models[self.app_label] 
Example #3
Source File: test_commands.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def tearDown(self):
        apps.app_configs['migrations'].models = self._old_models
        apps.all_models['migrations'] = self._old_models
        apps.clear_cache()
        super().tearDown() 
Example #4
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_egg4(self):
        """Loading an app with no models from under the top-level egg package generates no error"""
        egg_name = '%s/omelet.egg' % self.egg_dir
        with extend_sys_path(egg_name):
            with self.settings(INSTALLED_APPS=['omelet.app_no_models']):
                models_module = apps.get_app_config('app_no_models').models_module
                self.assertIsNone(models_module)
        del apps.all_models['app_no_models'] 
Example #5
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_egg3(self):
        """Models module can be loaded from an app located under an egg's top-level package"""
        egg_name = '%s/omelet.egg' % self.egg_dir
        with extend_sys_path(egg_name):
            with self.settings(INSTALLED_APPS=['omelet.app_with_models']):
                models_module = apps.get_app_config('app_with_models').models_module
                self.assertIsNotNone(models_module)
        del apps.all_models['app_with_models'] 
Example #6
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_egg2(self):
        """Loading an app from an egg that has no models returns no models (and no error)"""
        egg_name = '%s/nomodelapp.egg' % self.egg_dir
        with extend_sys_path(egg_name):
            with self.settings(INSTALLED_APPS=['app_no_models']):
                models_module = apps.get_app_config('app_no_models').models_module
                self.assertIsNone(models_module)
        del apps.all_models['app_no_models'] 
Example #7
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_egg1(self):
        """Models module can be loaded from an app in an egg"""
        egg_name = '%s/modelapp.egg' % self.egg_dir
        with extend_sys_path(egg_name):
            with self.settings(INSTALLED_APPS=['app_with_models']):
                models_module = apps.get_app_config('app_with_models').models_module
                self.assertIsNotNone(models_module)
        del apps.all_models['app_with_models'] 
Example #8
Source File: test_commands.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def tearDown(self):
        apps.app_configs['migrations'].models = self._old_models
        apps.all_models['migrations'] = self._old_models
        apps.clear_cache()
        super().tearDown() 
Example #9
Source File: conftest.py    From django-dynamic-models with MIT License 5 votes vote down vote up
def cleanup_registry():
    """
    The app registry bleeds between tests. This fixture removes all dynamically
    declared models after each test.
    """
    try:
        yield
    finally:
        app_config = apps.get_app_config(TEST_APP_LABEL)
        registered_models = app_config.get_models()
        apps.all_models[TEST_APP_LABEL].clear()
        apps.register_model(TEST_APP_LABEL, ModelSchema)
        apps.register_model(TEST_APP_LABEL, FieldSchema) 
Example #10
Source File: utils.py    From django-dynamic-models with MIT License 5 votes vote down vote up
def unregister_model(self, model_name):
        try:
            del apps.all_models[self.app_label][model_name.lower()]
        except KeyError as err:
            raise LookupError("'{}' not found.".format(model_name)) from err 
Example #11
Source File: test_fields.py    From django-osm-field with MIT License 5 votes vote down vote up
def tearDown(self):
        # Taken from IsolatedModelsTestCase in
        # django/tests/invalid_models_tests/base.py
        from django.apps import apps

        apps.app_configs["tests"].models = self._old_models
        apps.all_models["tests"] = self._old_models
        apps.clear_cache() 
Example #12
Source File: test_commands.py    From django-sqlserver with MIT License 5 votes vote down vote up
def tearDown(self):
        apps.app_configs['migrations'].models = self._old_models
        apps.all_models['migrations'] = self._old_models
        apps.clear_cache()
        super(MakeMigrationsTests, self).tearDown() 
Example #13
Source File: models.py    From django-collaborative with MIT License 5 votes vote down vote up
def create_models():
    """
    Build & register models from the DynamicModel descriptions found
    in our database.
    """
    for dynmodel in DynamicModel.objects.all():
        model_name = dynmodel.name
        _model = construct_model(dynmodel)
        if not _model:
            logger.error("No model was created for: %s" % model_name)
            continue

        these_models = apps.all_models["django_models_from_csv"]
        these_models[model_name] = _model 
Example #14
Source File: test_streamfield.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def tearDown(self):
        # unregister InvalidStreamModel from the overall model registry
        # so that it doesn't break tests elsewhere
        for package in ('wagtailcore', 'wagtail.core.tests'):
            try:
                del apps.all_models[package]['invalidstreammodel']
            except KeyError:
                pass
        apps.clear_cache() 
Example #15
Source File: test_settings.py    From django-tenants with MIT License 5 votes vote down vote up
def test_PG_EXTRA_SEARCH_PATHS(self):
        del apps.all_models['django_tenants']
        c = connection.cursor()
        c.execute('DROP SCHEMA {0} CASCADE; CREATE SCHEMA {0};'.format(
            get_public_schema_name()
        ))
        apps.set_installed_apps(['customers', 'django_tenants']) 
Example #16
Source File: tests.py    From django-seo with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def tearDown(self):
        del apps.all_models['djangoseo']['animal'] 
Example #17
Source File: tests.py    From django-seo with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def tearDown(self):
        del apps.all_models['djangoseo']['dog']