Permalink
Browse files

Fixed #12674 -- provide a way to override admin validation

Moved admin validation code to classes and have those be class
attributes to the ModelAdmin classes.
  • Loading branch information...
1 parent 886f7cc commit 4ad1eb1c14b629cf5bcfd253ed40e875f1bddd47 @HonzaKral HonzaKral committed Feb 23, 2013
@@ -10,6 +10,7 @@
from django.contrib.admin import widgets, helpers
from django.contrib.admin.util import (unquote, flatten_fieldsets, get_deleted_objects,
model_format_dict, NestedObjects)
+from django.contrib.admin import validation
from django.contrib.admin.templatetags.admin_static import static
from django.contrib import messages
from django.views.decorators.csrf import csrf_protect
@@ -87,6 +88,14 @@ class BaseModelAdmin(six.with_metaclass(RenameBaseModelAdminMethods)):
readonly_fields = ()
ordering = None
+ # validation
+ validator_class = validation.BaseValidator
+
+ @classmethod
+ def validate(cls, model):
+ validator = cls.validator_class()
+ validator.validate(cls, model)
+
def __init__(self):
overrides = FORMFIELD_FOR_DBFIELD_DEFAULTS.copy()
overrides.update(self.formfield_overrides)
@@ -371,6 +380,9 @@ class ModelAdmin(BaseModelAdmin):
actions_on_bottom = False
actions_selection_counter = True
+ # validation
+ validator_class = validation.ModelAdminValidator
+
def __init__(self, model, admin_site):
self.model = model
self.opts = model._meta
@@ -1447,6 +1459,9 @@ class InlineModelAdmin(BaseModelAdmin):
verbose_name_plural = None
can_delete = True
+ # validation
+ validator_class = validation.InlineValidator
+
def __init__(self, parent_model, admin_site):
self.admin_site = admin_site
self.parent_model = parent_model
@@ -66,12 +66,6 @@ def register(self, model_or_iterable, admin_class=None, **options):
if not admin_class:
admin_class = ModelAdmin
- # Don't import the humongous validation code unless required
- if admin_class and settings.DEBUG:
- from django.contrib.admin.validation import validate
- else:
- validate = lambda model, adminclass: None
-
if isinstance(model_or_iterable, ModelBase):
model_or_iterable = [model_or_iterable]
for model in model_or_iterable:
@@ -94,8 +88,8 @@ def register(self, model_or_iterable, admin_class=None, **options):
options['__module__'] = __name__
admin_class = type("%sAdmin" % model.__name__, (admin_class,), options)
- # Validate (which might be a no-op)
- validate(admin_class, model)
+ if admin_class is not ModelAdmin and settings.DEBUG:
+ admin_class.validate(model)
# Instantiate the admin class to save in the registry
self._registry[model] = admin_class(model, self)
Oops, something went wrong.

0 comments on commit 4ad1eb1

Please sign in to comment.