Opened 13 years ago

Closed 13 years ago

#17774 closed Bug (invalid)

In BooleanField default value is not set

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

Description (last modified by Ramiro Morales)

My code:

class Employee(models.Model):
    class Meta:
        verbose_name = _('Employee')
        verbose_name_plural = _('Employees')

    is_active = models.BooleanField(default=True, name=_('Active'), verbose_name=_('Active'))

class EmployeeForm(forms.ModelForm):
    class Meta:
        model = Employee

def create(self, request, *args, **kwargs):

    form = EmployeeForm()

    if form.is_valid():
        try:
            print "Form clean data is %s" % form.cleaned_data
            return form.save()
        except PermissionDenied:
            return rc.FORBIDDEN
        except Exception:
                return rc.INTERNAL_ERROR
    else:
        print form.errors.as_text()

Output is:

Form clean data is {'is_active': False}

Change History (4)

comment:1 by Ramiro Morales, 13 years ago

Description: modified (diff)

(reformatted description, please use the Preview button to check the format of your ticket description before submitting it)

comment:2 by Ramiro Morales, 13 years ago

Resolution: invalid
Status: newclosed

I can't reproduce this:

In [1]: from t17774.forms import EmployeeForm

In [2]: f = EmployeeForm()

In [3]: f.is_valid()
Out[3]: False

I'm using the sqlite DB backend.

is_valid() doesn't return True when instantiating an EmployeeForm without initial data to create a new Employee instance and without an associated existing Employee model instance to edit.

To the contributor that opened the ticket: reopen it if you can provide full details about your setup and if you can confirm the form handling code you are using is effectively the one you've pasted above.

comment:3 by anonymous, 13 years ago

Resolution: invalid
Status: closedreopened

Well, I'm use

Django==1.3.1

psycopg2==2.4.1

postgresql-server Version: 9.1.2-65.1 Arch: x86_64

In addition I'm tried insert into my form this string

    is_active = forms.BooleanField(required=False, initial=True)

but it does not give

comment:4 by Ramiro Morales, 13 years ago

Resolution: invalid
Status: reopenedclosed

Well, I can't reproduce with 1.3.1 and Postgres either.

Something I did nos stress enough in my previous comment is that there is not point and I see no way is_valid() can return True
for a ModelForm instance that has been instantiated like you did because you aren't validating any user-provided input. I suggest to read the relevant documentation: https://docs.djangoproject.com/en/1.3/topics/forms/#using-a-form-in-a-view and search for help in user support channels.

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