From 3cfdfb1c9238df28bd8edd9d134749f19cfd5833 Mon Sep 17 00:00:00 2001 From: Cuong Nguyen Date: Thu, 23 Feb 2023 20:33:11 +0700 Subject: [PATCH] add auto_register_model_admin function --- admin_extended/utils.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 admin_extended/utils.py diff --git a/admin_extended/utils.py b/admin_extended/utils.py new file mode 100644 index 0000000..c9db1ac --- /dev/null +++ b/admin_extended/utils.py @@ -0,0 +1,22 @@ +from admin_extended.base import ExtendedAdminModel +from django.contrib import admin +from django.apps import apps + + +# Automatically Register All Models +class DefaultModelAdmin(ExtendedAdminModel): + def __init__(self, model, admin_site): + self.list_display = [field.name for field in model._meta.fields] + super().__init__(model, admin_site) + + +def auto_register_model_admin(default_model_admin_class=DefaultModelAdmin, ignore_models=[]): + all_models = apps.get_models() + + for model in all_models: + try: + model_identity = f'{model._meta.model_name}.{model._meta.app_label}' + if not model.__module__.startswith('django') and model_identity not in ignore_models: + admin.site.register(model, default_model_admin_class) + except admin.sites.AlreadyRegistered: + pass