Code

Opened 3 years ago

Closed 3 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.

Attachments (0)

Change History (3)

comment:1 in reply to: ↑ description Changed 3 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 3 years ago by anonymous

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 Changed 3 years ago by kmtracey

  • Resolution set to invalid
  • Status changed from new to 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.