Ticket #16986: model-clean-errordict-2.diff

File model-clean-errordict-2.diff, 2.0 KB (added by David Foster, 13 years ago)
  • django/forms/models.py

     
    330330        try:
    331331            self.instance.clean()
    332332        except ValidationError, e:
    333             self._update_errors({NON_FIELD_ERRORS: e.messages})
     333            if e.has_message_dict:
     334                self._update_errors(e.message_dict)
     335            else:
     336                self._update_errors({NON_FIELD_ERRORS: e.messages})
    334337
    335338        # Validate uniqueness if needed.
    336339        if self._validate_unique:
  • django/core/exceptions.py

     
    5959            self.params = params
    6060            message = force_unicode(message)
    6161            self.messages = [message]
    62 
     62   
     63    @property
     64    def has_message_dict(self):
     65        return hasattr(self, 'message_dict')
     66   
    6367    def __str__(self):
    6468        # This is needed because, without a __str__(), printing an exception
    6569        # instance would result in this:
    6670        # AttributeError: ValidationError instance has no attribute 'args'
    6771        # See http://www.python.org/doc/current/tut/node10.html#handling
    68         if hasattr(self, 'message_dict'):
     72        if self.has_message_dict:
    6973            return repr(self.message_dict)
    7074        return repr(self.messages)
    7175
    7276    def __repr__(self):
    73         if hasattr(self, 'message_dict'):
     77        if self.has_message_dict:
    7478            return 'ValidationError(%s)' % repr(self.message_dict)
    7579        return 'ValidationError(%s)' % repr(self.messages)
    7680
    7781    def update_error_dict(self, error_dict):
    78         if hasattr(self, 'message_dict'):
     82        if self.has_message_dict:
    7983            if error_dict:
    8084                for k, v in self.message_dict.items():
    8185                    error_dict.setdefault(k, []).extend(v)
Back to Top