Opened 13 years ago

Closed 13 years ago

#16539 closed Bug (invalid)

ModelForms ignore default values when value is not supplied

Reported by: semarjt@… Owned by: nobody
Component: Forms Version: 1.3
Severity: Normal Keywords: Model|Form
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

A model with boolean fields has values set to False, regardless of default in model declaration.

This is only if ModelForms are used to create the instance

There is a working example here: https://github.com/joelsemar/formapp.git

this is a simple base case, easily runnable.

Change History (3)

in reply to:  description comment:1 by anonymous, 13 years ago

Replying to semarjt@…:

A model with boolean fields has values set to False, regardless of default in model declaration.

This is only if ModelForms are used to create the instance

There is a working example here: https://github.com/joelsemar/formapp.git

this is a simple base case, easily runnable.

bad url: https://github.com/joelsemar/formapp

sorry

comment:2 by anonymous, 13 years ago

I have the same problem. I think the problem is not in ModelForm, but in creating databse tables for models (syncdb command may be).
I have model with amount field (see declaration below) and after run manage.py syncdb in my MySQL database this field has not default value.

amount = models.DecimalField(_('amount'), \
           decimal_places=2, max_digits=10, \
           blank=True, default=0)

comment:3 by Karen Tracey, 13 years ago

Resolution: invalid
Status: newclosed

For the original problem description: your issue is with how HTML checkbox inputs work. If a checkbox is not checked, the browser supplies no value for it when posting the form. Therefore Django must interpret no value present for a checkbox widget in a form's data to be "not checked", or False. If you want to allow for not specifying a value falling back to a default of True, you'll have to use some type of widget other than a checkbox.

For the follow-up comment: Django uses the default value when it creates model instances. It does not propagate that default value to the SQL for the table. This is design decision, not a bug.

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