Python django.contrib.admin.autodiscover() Examples

The following are 4 code examples for showing how to use django.contrib.admin.autodiscover(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.contrib.admin , or try the search function .

Example 1
Project: django-bananas   Author: 5monkeys   File: syncpermissions.py    License: MIT License 6 votes vote down vote up
def handle_noargs(self, *args, **options):
        from bananas import admin
        from django.contrib import admin as django_admin
        from django.contrib.contenttypes.models import ContentType

        django_admin.autodiscover()

        for model, _ in admin.site._registry.items():
            if issubclass(getattr(model, "View", object), admin.AdminView):
                meta = model._meta

                ct, created = ContentType.objects.get_or_create(
                    app_label=meta.app_label, model=meta.object_name.lower()
                )

                if created:
                    print("Found new admin view: {} [{}]".format(ct.name, ct.app_label))

                for codename, name in model._meta.permissions:
                    p, created = Permission.objects.update_or_create(
                        codename=codename, content_type=ct, defaults=dict(name=name)
                    )
                    if created:
                        print("Created permission: {}".format(name)) 
Example 2
Project: django-hijack-admin   Author: arteria   File: test_checks.py    License: MIT License 5 votes vote down vote up
def test_check_custom_user_model_custom_admin(self):
            class CustomAdminSite(admin.AdminSite):
                pass

            _default_site = admin.site
            admin.site = CustomAdminSite()
            admin.autodiscover()

            admin.site.register(get_user_model(), HijackUserAdmin)

            warnings = checks.check_custom_user_model(HijackAdminConfig)
            self.assertFalse(warnings)

            admin.site.unregister(get_user_model())
            admin.site = _default_site 
Example 3
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_double_call_autodiscover(self):
        # The first time autodiscover is called, we should get our real error.
        with self.assertRaises(Exception) as cm:
            admin.autodiscover()
        self.assertEqual(str(cm.exception), "Bad admin module")

        # Calling autodiscover again should raise the very same error it did
        # the first time, not an AlreadyRegistered error.
        with self.assertRaises(Exception) as cm:
            admin.autodiscover()
        self.assertEqual(str(cm.exception), "Bad admin module") 
Example 4
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_double_call_autodiscover(self):
        # The first time autodiscover is called, we should get our real error.
        with self.assertRaises(Exception) as cm:
            admin.autodiscover()
        self.assertEqual(str(cm.exception), "Bad admin module")

        # Calling autodiscover again should raise the very same error it did
        # the first time, not an AlreadyRegistered error.
        with self.assertRaises(Exception) as cm:
            admin.autodiscover()
        self.assertEqual(str(cm.exception), "Bad admin module")