#5374: We need a validator for ModelAdmin classes
-------------------------------------+--------------------------------------
          Reporter:  jkocherhans     |         Owner:                
            Status:  new             |     Milestone:  1.0 alpha     
         Component:  Core framework  |       Version:  newforms-admin
        Resolution:                  |      Keywords:  nfa-blocker   
             Stage:  Accepted        |     Has_patch:  1             
        Needs_docs:  1               |   Needs_tests:  1             
Needs_better_patch:  1               |  
-------------------------------------+--------------------------------------
Comment (by mrts):

 Another note: the initial plan was to provide classmethods in the line of
 the following for easy overriding:

 {{{
 Index: django/contrib/admin/options.py
 ===================================================================
 --- django/contrib/admin/options.py     (revision 7877)
 +++ django/contrib/admin/options.py     (working copy)
 @@ -221,6 +221,103 @@
          return None
      declared_fieldsets = property(_declared_fieldsets)

 +    def validate(cls, model):
 +        """Does basic option validation."""
 +        # insert validation here
 +    validate = classmethod(validate)
 +
  class ModelAdmin(BaseModelAdmin):
      "Encapsulates all admin options and functionality for a given model."
      __metaclass__ = forms.MediaDefiningClass
 @@ -236,7 +333,7 @@
      save_on_top = False
      ordering = None
      inlines = []
 -
 +
      # Custom templates (designed to be over-ridden in subclasses)
      change_form_template = None
      change_list_template = None
 @@ -735,6 +832,12 @@
              "admin/object_history.html"
          ], context, context_instance=template.RequestContext(request))

 +    def validate(cls, model):
 +        """Does basic option validation."""
 +        super(ModelAdmin, cls).validate()
 +        # insert validation here
 +    validate = classmethod(validate)
 +
  class InlineModelAdmin(BaseModelAdmin):
      """
      Options for inline editing of ``model`` instances.
 @@ -779,6 +882,12 @@
          form = self.get_formset(request).form
          return [(None, {'fields': form.base_fields.keys()})]

 +    def validate(cls, model):
 +        """Does basic option validation."""
 +        super(InlineModelAdmin, cls).validate()
 +        # insert validation here
 +    validate = classmethod(validate)
 +
  class StackedInline(InlineModelAdmin):
      template = 'admin/edit_inline/stacked.html'

 Index: django/contrib/admin/sites.py
 ===================================================================
 --- django/contrib/admin/sites.py       (revision 7877)
 +++ django/contrib/admin/sites.py       (working copy)
 @@ -7,6 +7,7 @@
  from django.utils.text import capfirst
  from django.utils.translation import ugettext_lazy, ugettext as _
  from django.views.decorators.cache import never_cache
 +from django.conf import settings
  import base64
  import cPickle as pickle
  import datetime
 @@ -65,6 +66,7 @@

          If a model is already registered, this will raise
 AlreadyRegistered.
          """
 +        do_validate = admin_class and settings.DEBUG
          admin_class = admin_class or ModelAdmin
          # TODO: Handle options
          if isinstance(model_or_iterable, ModelBase):
 @@ -72,6 +74,8 @@
          for model in model_or_iterable:
              if model in self._registry:
                  raise AlreadyRegistered('The model %s is already
 registered' % model.__name__)
 +            if do_validate:
 +                admin_class.validate(model)
              self._registry[model] = admin_class(model, self)

      def unregister(self, model_or_iterable):
 }}}

 However, that would make `options.py` and `ModelAdmin` classes needlessly
 heavy-weight. Thus, validation was moved to a separate module that is
 loaded only in debug mode. Custom validation is still possible, as there
 are hooks that will call `validate()` classmethod if provided in the
 custom `ModelAdmin` class. Their usefulness or relevance remains a point
 of discussion, see previous note.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/5374#comment:13>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to