Code

Opened 2 years ago

Closed 2 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)

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}

Attachments (0)

Change History (4)

comment:1 Changed 2 years ago by ramiro

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

comment:2 Changed 2 years ago by ramiro

  • Resolution set to invalid
  • Status changed from new to closed

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

  • Resolution invalid deleted
  • Status changed from closed to reopened

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 Changed 2 years ago by ramiro

  • Resolution set to invalid
  • Status changed from reopened to closed

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.

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.