本文整理汇总了Python中django.contrib.admin.autodiscover方法的典型用法代码示例。如果您正苦于以下问题:Python admin.autodiscover方法的具体用法?Python admin.autodiscover怎么用?Python admin.autodiscover使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.admin
的用法示例。
在下文中一共展示了admin.autodiscover方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_noargs
# 需要导入模块: from django.contrib import admin [as 别名]
# 或者: from django.contrib.admin import autodiscover [as 别名]
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))
示例2: test_check_custom_user_model_custom_admin
# 需要导入模块: from django.contrib import admin [as 别名]
# 或者: from django.contrib.admin import autodiscover [as 别名]
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
示例3: test_double_call_autodiscover
# 需要导入模块: from django.contrib import admin [as 别名]
# 或者: from django.contrib.admin import autodiscover [as 别名]
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")