本文整理汇总了Python中django.contrib.admin.sites.AdminSite.unregister方法的典型用法代码示例。如果您正苦于以下问题:Python AdminSite.unregister方法的具体用法?Python AdminSite.unregister怎么用?Python AdminSite.unregister使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.admin.sites.AdminSite
的用法示例。
在下文中一共展示了AdminSite.unregister方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lazy_registration
# 需要导入模块: from django.contrib.admin.sites import AdminSite [as 别名]
# 或者: from django.contrib.admin.sites.AdminSite import unregister [as 别名]
def lazy_registration(self):
# First, directly handle models we don't want at all,
# as per the ``ADMIN_REMOVAL`` setting.
for model in getattr(settings, "ADMIN_REMOVAL", []):
try:
model = tuple(model.rsplit(".", 1))
exec("from %s import %s" % model)
except ImportError:
pass
else:
try:
AdminSite.unregister(self, eval(model[1]))
except NotRegistered:
pass
# Call register/unregister.
for name, deferred_args, deferred_kwargs in self._deferred:
getattr(AdminSite, name)(self, *deferred_args, **deferred_kwargs)
示例2: lazy_registration
# 需要导入模块: from django.contrib.admin.sites import AdminSite [as 别名]
# 或者: from django.contrib.admin.sites.AdminSite import unregister [as 别名]
def lazy_registration(self):
# First pick up any admin classes registered via decorator to the
# default admin site.
for model, admin in default_site._registry.items():
self._deferred.append(("register", (model, admin.__class__), {}))
# Call register/unregister.
for name, args, kwargs in self._deferred:
try:
getattr(AdminSite, name)(self, *args, **kwargs)
except (AlreadyRegistered, NotRegistered):
pass
# Then handle models we don't want at all,
# as per the ``ADMIN_REMOVAL`` setting.
for model in getattr(settings, "ADMIN_REMOVAL", []):
try:
model = tuple(model.rsplit(".", 1))
exec("from %s import %s" % model)
except ImportError:
pass
else:
try:
AdminSite.unregister(self, eval(model[1]))
except NotRegistered:
pass
示例3: ModelAdminTests
# 需要导入模块: from django.contrib.admin.sites import AdminSite [as 别名]
# 或者: from django.contrib.admin.sites.AdminSite import unregister [as 别名]
#.........这里部分代码省略.........
ma = ConcertAdmin(Concert, self.site)
self.assertEqual(list(ma.get_form(request).base_fields), ['extra', 'transport'])
class ConcertInline(TabularInline):
form = AdminConcertForm
model = Concert
fk_name = 'main_band'
can_delete = True
class BandAdmin(ModelAdmin):
inlines = [ConcertInline]
ma = BandAdmin(Band, self.site)
self.assertEqual(
list(list(ma.get_formsets_with_inlines(request))[0][0]().forms[0].fields),
['extra', 'transport', 'id', 'DELETE', 'main_band']
)
def test_log_actions(self):
ma = ModelAdmin(Band, self.site)
mock_request = MockRequest()
mock_request.user = User.objects.create(username='bill')
content_type = get_content_type_for_model(self.band)
tests = (
(ma.log_addition, ADDITION, {'added': {}}),
(ma.log_change, CHANGE, {'changed': {'fields': ['name', 'bio']}}),
(ma.log_deletion, DELETION, str(self.band)),
)
for method, flag, message in tests:
with self.subTest(name=method.__name__):
created = method(mock_request, self.band, message)
fetched = LogEntry.objects.filter(action_flag=flag).latest('id')
self.assertEqual(created, fetched)
self.assertEqual(fetched.action_flag, flag)
self.assertEqual(fetched.content_type, content_type)
self.assertEqual(fetched.object_id, str(self.band.pk))
self.assertEqual(fetched.user, mock_request.user)
if flag == DELETION:
self.assertEqual(fetched.change_message, '')
self.assertEqual(fetched.object_repr, message)
else:
self.assertEqual(fetched.change_message, str(message))
self.assertEqual(fetched.object_repr, str(self.band))
def test_get_autocomplete_fields(self):
class NameAdmin(ModelAdmin):
search_fields = ['name']
class SongAdmin(ModelAdmin):
autocomplete_fields = ['featuring']
fields = ['featuring', 'band']
class OtherSongAdmin(SongAdmin):
def get_autocomplete_fields(self, request):
return ['band']
self.site.register(Band, NameAdmin)
try:
# Uses autocomplete_fields if not overridden.
model_admin = SongAdmin(Song, self.site)
form = model_admin.get_form(request)()
self.assertIsInstance(form.fields['featuring'].widget.widget, AutocompleteSelectMultiple)
# Uses overridden get_autocomplete_fields
model_admin = OtherSongAdmin(Song, self.site)
form = model_admin.get_form(request)()
self.assertIsInstance(form.fields['band'].widget.widget, AutocompleteSelect)
finally:
self.site.unregister(Band)
def test_get_deleted_objects(self):
mock_request = MockRequest()
mock_request.user = User.objects.create_superuser(username='bob', email='[email protected]', password='test')
self.site.register(Band, ModelAdmin)
ma = self.site._registry[Band]
deletable_objects, model_count, perms_needed, protected = ma.get_deleted_objects([self.band], request)
self.assertEqual(deletable_objects, ['Band: The Doors'])
self.assertEqual(model_count, {'bands': 1})
self.assertEqual(perms_needed, set())
self.assertEqual(protected, [])
def test_get_deleted_objects_with_custom_has_delete_permission(self):
"""
ModelAdmin.get_deleted_objects() uses ModelAdmin.has_delete_permission()
for permissions checking.
"""
mock_request = MockRequest()
mock_request.user = User.objects.create_superuser(username='bob', email='[email protected]', password='test')
class TestModelAdmin(ModelAdmin):
def has_delete_permission(self, request, obj=None):
return False
self.site.register(Band, TestModelAdmin)
ma = self.site._registry[Band]
deletable_objects, model_count, perms_needed, protected = ma.get_deleted_objects([self.band], request)
self.assertEqual(deletable_objects, ['Band: The Doors'])
self.assertEqual(model_count, {'bands': 1})
self.assertEqual(perms_needed, {'band'})
self.assertEqual(protected, [])