#30596: Form method `has_changed` always returns True if form has the
SplitArrayField field
----------------------------------+--------------------------------------
     Reporter:  Evgeniy Krysanov  |                    Owner:  nobody
         Type:  Bug               |                   Status:  new
    Component:  Forms             |                  Version:  2.2
     Severity:  Normal            |               Resolution:
     Keywords:                    |             Triage Stage:  Unreviewed
    Has patch:  0                 |      Needs documentation:  0
  Needs tests:  0                 |  Patch needs improvement:  0
Easy pickings:  0                 |                    UI/UX:  0
----------------------------------+--------------------------------------

Comment (by Evgeniy Krysanov):

 Replying to [comment:1 felixxm]:
 > I was not able to reproduce this issue in basic tests. Maybe it is
 related `remove_trailing_nulls=True`, do you have them in a database? e.g.
 `[1.33, 5.33. 8.78, None]` Can you check initial data?

 I checked a lot of cases until I isolated the problem -
 https://github.com/django/django/blob/master/django/forms/forms.py#L451

 `initial_value` is the original object's value, for example [3.5, 1.0,
 2.2]. `data_value` is converted from form data to ['3.5', '1.0', '2.2'],
 but not converted to FloatField values. If the `initial_value` is [] then
 the `data_value` is `['', '', '']`. Thats why `has_changed` always returns
 True.

 I fixed this bug in my project with the following code:

 {{{
 class BetterSplitArrayField(SplitArrayField):
     def to_python(self, value):
         return self.clean(value)
 }}}

 When calling `field.has_changed(initial_value, data_value)`there is a call
 to `data = self.to_python(data)`
 https://github.com/django/django/blob/master/django/forms/fields.py#L181
 but `SplitArrayField` just return original value from this method. My code
 fix this.

 But it would be better to see the fix in Django source. I can write a PR
 with unitests if you wish.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30596#comment:2>
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/066.e9c5aab95f8ee4c3f7d20edd4d08a56d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to