mirror of
https://github.com/django/django.git
synced 2025-11-23 12:26:57 +00:00
Fixed #19414 -- Added admin registration decorator
Thanks stavros for the suggestion.
This commit is contained in:
parent
d1c9802811
commit
98514849dc
6 changed files with 127 additions and 1 deletions
|
|
@ -1,5 +1,6 @@
|
|||
# ACTION_CHECKBOX_NAME is unused, but should stay since its import from here
|
||||
# has been referenced in documentation.
|
||||
from django.contrib.admin.decorators import register
|
||||
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
|
||||
from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL
|
||||
from django.contrib.admin.options import StackedInline, TabularInline
|
||||
|
|
|
|||
28
django/contrib/admin/decorators.py
Normal file
28
django/contrib/admin/decorators.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
def register(*models, **kwargs):
|
||||
"""
|
||||
Registers the given model(s) classes and wrapped ModelAdmin class with
|
||||
admin site:
|
||||
|
||||
@register(Author)
|
||||
class AuthorAdmin(admin.ModelAdmin):
|
||||
pass
|
||||
|
||||
A kwarg of `site` can be passed as the admin site, otherwise the default
|
||||
admin site will be used.
|
||||
"""
|
||||
from django.contrib.admin import ModelAdmin
|
||||
from django.contrib.admin.sites import site, AdminSite
|
||||
|
||||
def _model_admin_wrapper(admin_class):
|
||||
admin_site = kwargs.pop('site', site)
|
||||
|
||||
if not isinstance(admin_site, AdminSite):
|
||||
raise ValueError('site must subclass AdminSite')
|
||||
|
||||
if not issubclass(admin_class, ModelAdmin):
|
||||
raise ValueError('Wrapped class must sublcass ModelAdmin.')
|
||||
|
||||
admin_site.register(models, admin_class=admin_class)
|
||||
|
||||
return admin_class
|
||||
return _model_admin_wrapper
|
||||
Loading…
Add table
Add a link
Reference in a new issue