admin.register()

register(*models, site=django.admin.sites.site) [source]

There is also a decorator for registering your ModelAdmin classes:

from django.contrib import admin
from .models import Author

@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
    pass

It is given one or more model classes to register with the ModelAdmin and an optional keyword argument site if you are not using the default AdminSite:

from django.contrib import admin
from .models import Author, Reader, Editor
from myproject.admin_site import custom_admin_site

@admin.register(Author, Reader, Editor, site=custom_admin_site)
class PersonAdmin(admin.ModelAdmin):
    pass

You can’t use this decorator if you have to reference your model admin class in its __init__() method, e.g. super(PersonAdmin, self).__init__(*args, **kwargs). If you are using Python 3 and don’t have to worry about supporting Python 2, you can use super().__init__(*args, **kwargs) . Otherwise, you’ll have to use admin.site.register() instead of this decorator.

doc_Django
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.