#27992: Cleaned form values does not save to model
----------------------------------------+-----------------------------
               Reporter:  Pavel Patrin  |          Owner:  nobody
                   Type:  Bug           |         Status:  new
              Component:  Forms         |        Version:  1.10
               Severity:  Normal        |       Keywords:  model, form
           Triage Stage:  Unreviewed    |      Has patch:  0
    Needs documentation:  0             |    Needs tests:  0
Patch needs improvement:  0             |  Easy pickings:  0
                  UI/UX:  0             |
----------------------------------------+-----------------------------
 Example

 {{{
 class MyModel(Model):
     my_field = TextField(default='')


 class MyModelForm(ModelForm):
     class Meta:
         model = MyModel
         fields = ('my_field',)

     def clean_my_field(self):
         return 'cleaned_value'  # Or complex logic


 form = MyModelForm({})
 form.save()

 result = MyModelForm.objects.get()

 # This is true
 result.my_field != 'cleaned_value'
 }}}


 This happens, because Django applies model attributes like this:

 {{{
 def construct_instance(form, instance, fields=None, exclude=None):
     # ... code
     for f in opts.fields:
         # ... code
         # Leave defaults for fields that aren't in POST data, except for
         # checkbox inputs because they don't appear in POST data if not
 checked.
         if (f.has_default() and
 form[f.name].field.widget.value_omitted_from_data(form.data, form.files,
 form.add_prefix(f.name))):
             continue
         # ... code
     # ... code
     return instance
 }}}

 So Django skips fields that has default value, but not present in raw
 data. Why?

--
Ticket URL: <https://code.djangoproject.com/ticket/27992>
Django <https://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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/054.934863f57d46a38d0709422e60336228%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to