Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#23760 closed Cleanup/optimization (invalid)

ModelForm wrong default value on field without fields Meta attribute neither initial data.

Reported by: aRkadeFR Owned by: nobody
Component: Forms Version: 1.6
Severity: Normal Keywords: form, modelform, default, value
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I haven't found any similar ticket.

Create a Model with a BooleanField, null=False, default=True, blank=True.

Create the ModelForm with the Meta attribute model = ModelClass

Then if you want to create a ModelClass through the ModelForm(data={ XXXX without the BooleanField } ) , this will setup the booleanfield to False .

This is pretty unconvenient when upgrading a model without touching the forms.

Change History (4)

comment:1 by Tim Graham, 10 years ago

Resolution: invalid
Status: newclosed

Model field defaults are used for Form.initial, not if the user doesn't provide a value in the data.

comment:2 by aRkadeFR, 10 years ago

More explanation cause I think there's a real problem.

From this code with the good migration etc.

class Momo(models.Model):
    active = models.BooleanField('Active or not',
                                 null=False,
                                 blank=True,
                                 default=True,)
    comment = models.CharField('comment',
                               max_length=1024,
                               null=False,
                               default='default comment',
                               blank=True,)
    other = models.BooleanField('Nothing interesting',
                                blank=True,
                                default=False,)
class MomoForm(ModelForm):
    class Meta:
        model = models.Momo
        exclude = ('other', )

now, run into python shell:

In [3]: form = forms.MomoForm(data={'comment': 'test test comment :) '})                                                                              

In [4]: momo = form.save()

In [5]: momo.active
Out[5]: False

This should create an object with the default value of the active flag, and then hydrate it with the data provided.
Instead, it create with a False value.

From the user point of view, it's not friendly. Cause if in the template you don't render a form input or your missing a value in the data, it creates the wrong value.

comment:3 by Tim Graham, 10 years ago

"if in the template you don't render a form input or your missing a value in the data, it creates the wrong value."

That's considered an error by the developer. You shouldn't include the field on the form if you don't intend to render or populate it.

comment:4 by Matthew Schinckel, 10 years ago

For what it's worth, if you need to know if something has been explicitly selected as Yes/No, then you _can_ write a form field that renders as a radio input, with two selections, neither of which is initially selected. Probably similar in concept to a NullBooleanField.

You may even be able to use a NullBooleanField, and have custom form logic that rejects the None under certain circumstances.

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