2017-02-02 00:41:56 +08:00
|
|
|
def register(*models, site=None):
|
2012-12-18 08:04:10 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Register the given model(s) classes and wrapped ModelAdmin class with
|
2012-12-18 08:04:10 +08:00
|
|
|
admin site:
|
|
|
|
|
|
|
|
@register(Author)
|
|
|
|
class AuthorAdmin(admin.ModelAdmin):
|
|
|
|
pass
|
|
|
|
|
2017-02-02 00:41:56 +08:00
|
|
|
The `site` kwarg is an admin site to use instead of the default admin site.
|
2012-12-18 08:04:10 +08:00
|
|
|
"""
|
|
|
|
from django.contrib.admin import ModelAdmin
|
2017-02-02 00:41:56 +08:00
|
|
|
from django.contrib.admin.sites import site as default_site, AdminSite
|
2012-12-18 08:04:10 +08:00
|
|
|
|
|
|
|
def _model_admin_wrapper(admin_class):
|
2015-11-06 14:21:29 +08:00
|
|
|
if not models:
|
|
|
|
raise ValueError('At least one model must be passed to register.')
|
|
|
|
|
2017-02-02 00:41:56 +08:00
|
|
|
admin_site = site or default_site
|
2012-12-18 08:04:10 +08:00
|
|
|
|
|
|
|
if not isinstance(admin_site, AdminSite):
|
|
|
|
raise ValueError('site must subclass AdminSite')
|
|
|
|
|
|
|
|
if not issubclass(admin_class, ModelAdmin):
|
2013-09-19 18:24:12 +08:00
|
|
|
raise ValueError('Wrapped class must subclass ModelAdmin.')
|
2012-12-18 08:04:10 +08:00
|
|
|
|
|
|
|
admin_site.register(models, admin_class=admin_class)
|
|
|
|
|
|
|
|
return admin_class
|
|
|
|
return _model_admin_wrapper
|