Automatic admins registration

67 views
Skip to first unread message

Vladimir Godovalov

unread,
Dec 18, 2019, 10:19:47 AM12/18/19
to Django developers (Contributions to Django itself)

Hello to everyone,


# This non-trivial patch improve Django functionality
# by providing automatic registration in main admin.py
# of all admin files of the project



####  admin.py module code  ####

# admin.py
import importlib
from django.contrib import admin
from django.apps import apps


# automatic registration module
models = apps.get_models()
for model in models:
    model_name = str(model.__name__)

    # import and registration
    app_list = model.__module__.split(".")
    idx = app_list.index('models')
    app1 = app_list[idx-1]
    del app_list[idx:]

    try:
        module = importlib.import_module('.'.join(app_list)+'.'+app1.title()+'Admin')
    except ImportError:
        continue
    class_admin = module.str_to_class(model_name+'Admin')
    if class_admin is None:
        continue
    try:
        admin.site.register(model, class_admin)
    except admin.sites.AlreadyRegistered:
        pass


###  an application admin module code  ###
# make sure the module name looks like 'BaseAdmin.py',
# where the module name begins with the application
# name in uppercase as well as the word 'Admin'

# BaseAdmin.py
import sys

....

insert your code here

....


def str_to_class(str):
    try:
        obj = getattr(sys.modules[__name__], str)
    except AttributeError:
        return None
    return obj

Adam Johnson

unread,
Dec 18, 2019, 11:46:18 AM12/18/19
to django-d...@googlegroups.com
Hi Vladimir

Posting code to this mailing list is the wrong way to try make contributions. Thanks for offering though.

Please read the guides at https://docs.djangoproject.com/en/dev/internals/contributing/ for how to post tickets and code.

However from a quick skim I'm not sure your code is valuable to add to Django core at this time.

Thanks,

Adam

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/1fddfa20-339f-463a-a168-e86d8bb3f739%40googlegroups.com.


--
Adam

Vladimir Godovalov

unread,
Dec 18, 2019, 12:45:25 PM12/18/19
to Django developers (Contributions to Django itself)


среда, 18 декабря 2019 г., 19:46:18 UTC+3 пользователь Adam Johnson написал:
To unsubscribe from this group and stop receiving emails from it, send an email to django-d...@googlegroups.com.


--
Adam
 
 
Thanks Adam, I will do it the right way.
Reply all
Reply to author
Forward
0 new messages