[Django] #27433: ModelForm with BooleanField can not save False/unchecked

21 views
Skip to first unread message

Django

unread,
Nov 4, 2016, 8:56:46 AM11/4/16
to django-...@googlegroups.com
#27433: ModelForm with BooleanField can not save False/unchecked
----------------------------------------------+------------------------
Reporter: Christian Pedersen | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
----------------------------------------------+------------------------
My model has this field
{{{
starred_only = models.BooleanField(default=False)
}}}

Overriden in the ModelForm
{{{
starred_only = forms.BooleanField(required=False)
}}}

Django 1.10 stopped saving the unchecked (False) value. 1.10.1 started
doing it again, and from 1.10.2 it stopped.

This fixed it: https://github.com/django/django/pull/7068
And this broke it: https://github.com/django/django/pull/7217

I tried creating a custom widget:

{{{
class WorkingCheckboxInput(CheckboxInput):
def value_omitted_from_data(self, data, files, name):
return True
}}}

but value_omitted_from_data is never called.

The docs and/or minor release changelogs doesn't really document how to
get the previous default behavior back.

--
Ticket URL: <https://code.djangoproject.com/ticket/27433>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 5, 2016, 7:03:23 AM11/5/16
to django-...@googlegroups.com
#27433: ModelForm with BooleanField can not save False/unchecked
-------------------------------------+-------------------------------------
Reporter: Christian Pedersen | Owner: Patricia
| Sousa
Type: Bug | Status: assigned
Component: Forms | Version: 1.10
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
-------------------------------------+-------------------------------------
Changes (by Patricia Sousa):

* owner: nobody => Patricia Sousa
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/27433#comment:1>

Django

unread,
Nov 5, 2016, 7:20:34 AM11/5/16
to django-...@googlegroups.com
#27433: ModelForm with BooleanField can not save False/unchecked
------------------------------------+--------------------------------------
Reporter: Christian Pedersen | Owner: (none)

Type: Bug | Status: new
Component: Forms | Version: 1.10
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
------------------------------------+--------------------------------------
Changes (by Patricia Sousa):

* owner: Patricia Sousa => (none)
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/27433#comment:2>

Django

unread,
Nov 5, 2016, 9:18:23 AM11/5/16
to django-...@googlegroups.com
#27433: ModelForm with BooleanField can not save False/unchecked
-------------------------------------+-------------------------------------
Reporter: Christian Pedersen | Owner: Ingo
| Klöcker
Type: Bug | Status: assigned
Component: Forms | Version: 1.10
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
-------------------------------------+-------------------------------------
Changes (by Ingo Klöcker):

* status: new => assigned

* owner: (none) => Ingo Klöcker


--
Ticket URL: <https://code.djangoproject.com/ticket/27433#comment:3>

Django

unread,
Nov 5, 2016, 11:50:58 AM11/5/16
to django-...@googlegroups.com
#27433: ModelForm with BooleanField can not save False/unchecked
-------------------------------------+-------------------------------------
Reporter: Christian Pedersen | Owner: Ingo
| Klöcker
Type: Bug | Status: closed
Component: Forms | Version: 1.10
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ingo Klöcker):

* status: assigned => closed
* resolution: => needsinfo


Comment:

I could not reproduce the problem. The following test passes with Django
1.10.2.

{{{
class Test(TestCase):
def test_27433(self):
class Model(models.Model):
starred_only = models.BooleanField(default=False)

class Form(forms.ModelForm):
starred_only = forms.BooleanField(required=False)

class Meta:
model = Model
fields = ['starred_only']

m = Model(starred_only=True)
f = Form({}, instance=m)
self.assertTrue(f.is_valid())
self.assertFalse(m.starred_only)
}}}

The test fails if I change
{{{
fields = ['starred_only']
}}}
to
{{{
fields = []
}}}
but I think that's expected behavior.

See also https://docs.djangoproject.com/en/1.10/topics/forms/modelforms
/#the-save-method (which I think you did already read).

Please provide a test case that demonstrates your bug.

--
Ticket URL: <https://code.djangoproject.com/ticket/27433#comment:4>

Django

unread,
Nov 6, 2016, 2:09:36 PM11/6/16
to django-...@googlegroups.com
#27433: ModelForm with BooleanField can not save False/unchecked
-------------------------------------+-------------------------------------
Reporter: Christian Pedersen | Owner: Ingo
| Klöcker
Type: Bug | Status: closed
Component: Forms | Version: 1.10
Severity: Normal | Resolution: needsinfo

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 Christian Pedersen):

This also happens in the Django Admin. I can not untick/mark false any
records with a BooleanField(default=False)

The form is correctly marked as False, however the model is never updated
from True -> False when calling save

--
Ticket URL: <https://code.djangoproject.com/ticket/27433#comment:5>

Django

unread,
Nov 6, 2016, 2:09:44 PM11/6/16
to django-...@googlegroups.com
#27433: ModelForm with BooleanField can not save False/unchecked
-------------------------------------+-------------------------------------
Reporter: Christian Pedersen | Owner: Ingo
| Klöcker
Type: Bug | Status: new
Component: Forms | Version: 1.10
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
-------------------------------------+-------------------------------------
Changes (by Christian Pedersen):

* status: closed => new
* resolution: needsinfo =>


--
Ticket URL: <https://code.djangoproject.com/ticket/27433#comment:6>

Django

unread,
Nov 6, 2016, 11:13:02 PM11/6/16
to django-...@googlegroups.com
#27433: ModelForm with BooleanField can not save False/unchecked
-------------------------------------+-------------------------------------
Reporter: Christian Pedersen | Owner: Ingo
| Klöcker
Type: Bug | Status: new
Component: Forms | Version: 1.10
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 Tim Graham):

I also cannot reproduce a problem here. Please more details about how to
reproduce it, perhaps with a sample project. How I tried:

{{{
class Bool(models.Model):
starred_only = models.BooleanField(default=False)

class BoolForm(forms.ModelForm):
starred_only = forms.BooleanField(required=False)

class Meta:
fields = '__all__'

admin.site.register(Bool, form=BoolForm)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27433#comment:7>

Django

unread,
Nov 7, 2016, 4:52:23 AM11/7/16
to django-...@googlegroups.com
#27433: ModelForm with BooleanField can not save False/unchecked
-------------------------------------+-------------------------------------
Reporter: Christian Pedersen | Owner: Ingo
| Klöcker
Type: Bug | Status: closed
Component: Forms | Version: 1.10
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Christian Pedersen):

* status: new => closed
* resolution: => invalid


Comment:

I found the problem. It turns out django-cassandra-engine overrides the
construct_instance method.

Change

{{{
if (field_has_default and form.add_prefix(f.name) not in form.data
and
not getattr(form[f.name].field.widget,
'dont_use_model_field_default_for_empty_data',
False)):
continue

}}}

to

{{{
if (field_has_default and
form[f.name].field.widget.value_omitted_from_data(form.data, form.files,
form.add_prefix(f.name))):
continue
}}}

That explains why it works in 1.10.1 only.

--
Ticket URL: <https://code.djangoproject.com/ticket/27433#comment:8>

Reply all
Reply to author
Forward
0 new messages