Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

decorators.py 969B

123456789101112131415161718192021222324252627282930
  1. def register(*models, site=None):
  2. """
  3. Register the given model(s) classes and wrapped ModelAdmin class with
  4. admin site:
  5. @register(Author)
  6. class AuthorAdmin(admin.ModelAdmin):
  7. pass
  8. The `site` kwarg is an admin site to use instead of the default admin site.
  9. """
  10. from django.contrib.admin import ModelAdmin
  11. from django.contrib.admin.sites import site as default_site, AdminSite
  12. def _model_admin_wrapper(admin_class):
  13. if not models:
  14. raise ValueError('At least one model must be passed to register.')
  15. admin_site = site or default_site
  16. if not isinstance(admin_site, AdminSite):
  17. raise ValueError('site must subclass AdminSite')
  18. if not issubclass(admin_class, ModelAdmin):
  19. raise ValueError('Wrapped class must subclass ModelAdmin.')
  20. admin_site.register(models, admin_class=admin_class)
  21. return admin_class
  22. return _model_admin_wrapper