Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#28387 closed Bug (fixed)

Disabled fields should not be considered changed in bound forms

Reported by: Kevin Corbin Owned by: Srinivas Reddy Thatiparthy
Component: Forms Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

This is similar to #27431, but that ticket only modified the has_changed method in the base django.forms.fields.Field class.

Other form field classes included in django.forms.fields and django.forms.models override the base class' has_changed method, so aren't checking for the disabled attribute when determining whether or not a bound form field's data has changed.

As of Django 1.11.3, the following are affected:
*django.forms.fields.BooleanField
*django.forms.fields.MultipleChoiceField
*django.forms.fields.MultiValueField
*django.forms.fields.FileField
*django.forms.models.ModelChoiceField
*django.forms.models.ModelMultipleChoiceField

Here is an example form class that exhibits this behavior:

from django.contrib.auth import get_user_model
class MyTestForm(forms.Form):
    test_disabled = forms.ModelChoiceField(disabled=True, 
                                           queryset=get_user_model().objects.all(), 
                                           initial=get_user_model().objects.first().id)

From the console I show the bug as such:

form = MyTestForm(data={})
form.is_bound #True
form.has_changed() #True
form.changed_data #['test_disabled']

Change History (6)

comment:1 by Srinivas Reddy Thatiparthy, 7 years ago

Owner: changed from nobody to Srinivas Reddy Thatiparthy
Status: newassigned

Will submit a PR soon.

Thanks,
Srini

comment:2 by Srinivas Reddy Thatiparthy, 7 years ago

comment:3 by Srinivas Reddy Thatiparthy, 7 years ago

Has patch: set

comment:4 by Tim Graham, 7 years ago

Patch needs improvement: set
Triage Stage: UnreviewedAccepted

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

Resolution: fixed
Status: assignedclosed

In 5debbdfc:

Fixed #28387 -- Fixed has_changed() for disabled form fields that subclass it.

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

In a3b5df8:

[1.11.x] Fixed #28387 -- Fixed has_changed() for disabled form fields that subclass it.

Backport of 5debbdfcc84266703191e084914998e38f5f52eb from master

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