Opened 13 years ago
Closed 13 years ago
#16539 closed Bug (invalid)
ModelForms ignore default values when value is not supplied
Reported by: | 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)
comment:1 by , 13 years ago
comment:2 by , 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 , 13 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
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.
Replying to semarjt@…:
bad url: https://github.com/joelsemar/formapp
sorry