Opened 18 months ago

Closed 18 months ago

Last modified 18 months 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 Changed 18 months ago by Srinivas Reddy Thatiparthy

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

Will submit a PR soon.

Thanks,
Srini

comment:2 Changed 18 months ago by Srinivas Reddy Thatiparthy

comment:3 Changed 18 months ago by Srinivas Reddy Thatiparthy

Has patch: set

comment:4 Changed 18 months ago by Tim Graham

Patch needs improvement: set
Triage Stage: UnreviewedAccepted

comment:5 Changed 18 months ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 5debbdfc:

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

comment:6 Changed 18 months ago by Tim Graham <timograham@…>

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