运行 AppRegistry 初始化后的系统检查 - Django Admin
Running a system check after AppRegistry is initialized - Django Admin
我正在寻找 运行 特定检查,以确保项目(多个应用程序)中使用的 Django Admin classes(ModelAdmin、TabularInline 等)正在使用或继承来自 class(在本例中为 mixin)- 尽管系统检查会失败,因为 AppRegistry
尚未加载。
我目前正在使用以下内容;尽管这表明 AppRegistry
未加载。
from django.contrib.admin.sites import all_sites
from django.core.checks import register, Error
@register()
def check_django_admin_inheritance(app_configs, **kwargs):
errors = []
for admin_site in all_sites:
for model, admin in admin_site._registry.items():
if MyMixin not in admin.__class__.__bases__:
errors.append(
Error('All admin sites should derive from the MyMixin (common.django_admin)',
hint='Inherit from MyMixin or use our custom ModelAdmin (common.django_admin)',
obj=admin, id="id-here")
)
return errors
有没有其他办法解决这个问题;除了 AppConfig.ready()
这需要我将它放在每个应用程序中 - 我更期待一个干净和集中的解决方案。
您可以简单地在一些合适的应用程序 的 AppConfig.ready()
中注册您的支票。你还写 errors.append([Error(...)])
这意味着你将错误列表附加到你应该 return 的列表中,这会给你一个错误:
from django.contrib.admin.sites import all_sites
from django.core.checks import register, Error
def check_django_admin_inheritance(app_configs, **kwargs):
errors = []
for admin_site in all_sites:
for model, admin in admin_site._registry.items():
if MyMixin not in admin.__class__.__bases__:
errors.append(
Error('All admin sites should derive from the MyMixin (common.django_admin)',
hint='Inherit from MyMixin or use our custom ModelAdmin (common.django_admin)',
obj=admin, id="id-here")
)
return errors
class MyAppConfig(AppConfig):
...
def ready(self):
register(check_django_admin_inheritance) # Register the check here
我在我自己的应用程序中编写了这个,并且检查给出了 auth
应用程序的 User
和 Group
的错误消息,所以这按预期工作。
我正在寻找 运行 特定检查,以确保项目(多个应用程序)中使用的 Django Admin classes(ModelAdmin、TabularInline 等)正在使用或继承来自 class(在本例中为 mixin)- 尽管系统检查会失败,因为 AppRegistry
尚未加载。
我目前正在使用以下内容;尽管这表明 AppRegistry
未加载。
from django.contrib.admin.sites import all_sites
from django.core.checks import register, Error
@register()
def check_django_admin_inheritance(app_configs, **kwargs):
errors = []
for admin_site in all_sites:
for model, admin in admin_site._registry.items():
if MyMixin not in admin.__class__.__bases__:
errors.append(
Error('All admin sites should derive from the MyMixin (common.django_admin)',
hint='Inherit from MyMixin or use our custom ModelAdmin (common.django_admin)',
obj=admin, id="id-here")
)
return errors
有没有其他办法解决这个问题;除了 AppConfig.ready()
这需要我将它放在每个应用程序中 - 我更期待一个干净和集中的解决方案。
您可以简单地在一些合适的应用程序 的 AppConfig.ready()
中注册您的支票。你还写 errors.append([Error(...)])
这意味着你将错误列表附加到你应该 return 的列表中,这会给你一个错误:
from django.contrib.admin.sites import all_sites
from django.core.checks import register, Error
def check_django_admin_inheritance(app_configs, **kwargs):
errors = []
for admin_site in all_sites:
for model, admin in admin_site._registry.items():
if MyMixin not in admin.__class__.__bases__:
errors.append(
Error('All admin sites should derive from the MyMixin (common.django_admin)',
hint='Inherit from MyMixin or use our custom ModelAdmin (common.django_admin)',
obj=admin, id="id-here")
)
return errors
class MyAppConfig(AppConfig):
...
def ready(self):
register(check_django_admin_inheritance) # Register the check here
我在我自己的应用程序中编写了这个,并且检查给出了 auth
应用程序的 User
和 Group
的错误消息,所以这按预期工作。