Opened 14 years ago

Closed 12 years ago

Last modified 12 years ago

#14885 closed Cleanup/optimization (fixed)

is_valid for ModelForm changes instance if instance is provided

Reported by: italomaia Owned by: stumbles
Component: Documentation Version: 1.2
Severity: Normal Keywords: forms, is_valid, bad data
Cc: Torsten Bronger Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

See this example:

def val_check(value):
  from django.core.exceptions import ValidationError
  raise ValidationError('Some reason')

class Model(models.Model):
  image = models.ImageField(upload_to='some/place', validators=[val_check(])

class ModelForm(forms.ModelForm):
  pass

def view(request, model_id):
  instance = Model.objects.get(pk=model_id)
  if request.method=='POST':
    form = ModelForm(request.POST, instance=instance)
  else: form = ModelForm(instance=instance)
  if form.is_bound and form.is_valid():
    form.save()
    # do something else
  return simple.direct_to_template(request, {'template':'some.html', 'extra_context':{'model':instance}})

In the example above, if i send a post request with some data for the field image, instance will be modified. If i use the value of instance in my 'some.html' i'll get invalid data in my template. This seems wrong.

Attachments (1)

trac#14885-is_valid.diff (1.1 KB ) - added by stumbles 12 years ago.
Explain that is_valid() changes the model instance in place.

Download all attachments as: .zip

Change History (8)

comment:1 by anonymous, 14 years ago

Resolution: invalid
Status: newclosed

It is not clear from the report what this ticket is reporting, it doesn't help at all that the example code isn't correct/complete:

  • The view isn't instanciating the ModelForm with request-FILES, something absolutely necessary when dealing with file uploads.
  • The ModelForm definition is incomplete.
  • The code seems to be using some third party simple.direct_to_template() function.
  • The validators argument value has a syntax error.

If the OP or anyone wants to reopen this, please provide a working test case and a clear description of the issue at hand.

Remember Since Django 1.2 ModelFom validation also triggers Model validation

comment:2 by Ramiro Morales, 14 years ago

Oops, anonymous that close the ticket was me.

comment:3 by Torsten Bronger, 13 years ago

Cc: Torsten Bronger added
Component: UncategorizedForms
Easy pickings: unset
Resolution: invalid
Severity: Normal
Status: closedreopened
Type: Bug
UI/UX: unset

If necessary I will create a snippet for reproducing this, but actually the summary is complete: If you pass an "instance" to a model form constructor and later on call ".is_valid()" on that form, that given instance is changed in place. Very often you don't care about this object. But for example, if you want to compare the given instance with the new one returned by my_model_form.save(), changes are never detected.

I cannot read this behaviour from the docs. It is not about model validation here: The fields are really updated and not just normalised.

See also http://permalink.gmane.org/gmane.comp.python.django.user/132931 and http://groups.google.com/group/django-users/msg/2e31bfaefd8351cf.

comment:4 by Aymeric Augustin, 13 years ago

Component: FormsDocumentation
Easy pickings: set
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Accepting as a documentation issue. We could make it clearer that calling is_valid() will update the instance passed to the ModelForm.

by stumbles, 12 years ago

Attachment: trac#14885-is_valid.diff added

Explain that is_valid() changes the model instance in place.

comment:5 by stumbles, 12 years ago

Has patch: set
Owner: changed from nobody to stumbles
Status: reopenednew

I've added a patch to point out both that is_valid() causes the model instance to be modified in place, and also that this modification can be partial.

comment:6 by Tim Graham <timograham@…>, 12 years ago

Resolution: fixed
Status: newclosed

In [3fd89d99036696ba08dd2dd7e20a5b375f85d23b]:

Fixed #14885 - Clarified that ModelForm cleaning may not fully complete if the form is invalid.

Thanks Ben Sturmfels for the patch.

comment:7 by Tim Graham <timograham@…>, 12 years ago

In [42aee6ffe5f16852347e0cf069447950e9d2ef85]:

[1.4.x] Fixed #14885 - Clarified that ModelForm cleaning may not fully complete if the form is invalid.

Thanks Ben Sturmfels for the patch.

Backport of 3fd89d99036696ba08dd2dd7e20a5b375f85d23b from master.

Note: See TracTickets for help on using tickets.
Back to Top