【发布时间】:2021-07-14 13:31:35
【问题描述】:
我正在寻找进行特定检查,以确保项目中(多个应用程序中)使用的 Django 管理类(ModelAdmin、TabularInline 等)正在使用或继承自一个类(在这种情况下是一个 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() 之外,这需要我将它放在每个应用程序中 - 我更希望找到一个干净且集中的解决方案。
【问题讨论】:
标签: python python-3.x django django-admin