Ticket #13184: forms_models.diff

File forms_models.diff, 1.8 KB (added by Mark, 5 years ago)
  • django/forms/models.py

     
    3939    opts = instance._meta
    4040
    4141    cleaned_data = form.cleaned_data
     42    errors = {}
    4243    file_field_list = []
    4344    for f in opts.fields:
    4445        if not f.editable or isinstance(f, models.AutoField) \
     
    5354        if isinstance(f, models.FileField):
    5455            file_field_list.append(f)
    5556        else:
    56             f.save_form_data(instance, cleaned_data[f.name])
     57            try:
     58                f.save_form_data(instance, cleaned_data[f.name])
     59            except ValidationError, e:
     60                errors[f.name] = e.messages
    5761
    5862    for f in file_field_list:
    59         f.save_form_data(instance, cleaned_data[f.name])
     63        try:
     64            f.save_form_data(instance, cleaned_data[f.name])
     65        except ValidationError, e:
     66                errors[f.name] = e.messages
     67 
     68    if errors:
     69        raise ValidationError(errors)
    6070
    6171    return instance
    6272
     
    320330        opts = self._meta
    321331
    322332        # Update the model instance with self.cleaned_data.
    323         self.instance = construct_instance(self, self.instance, opts.fields, opts.exclude)
     333        to_python_errors = []
     334        try:
     335            self.instance = construct_instance(self, self.instance, opts.fields, opts.exclude)
     336        except ValidationError, e:
     337            to_python_errors = e.message_dict.keys()
     338            self._update_errors(e.message_dict)
    324339
    325340        # Clean the model instance's fields.
    326341        try:
    327             self.instance.clean_fields(exclude=exclude)
     342            self.instance.clean_fields(exclude=exclude+to_python_errors)
    328343        except ValidationError, e:
    329344            self._update_errors(e.message_dict)
    330345
Back to Top