Opened 16 years ago

Closed 16 years ago

Last modified 9 years ago

#9640 closed (fixed)

BooleanField with choices, defaults, and blank=False still given blank option in their formfields

Reported by: Nate Owned by: fperetti
Component: Forms Version: 1.0
Severity: Keywords: BooleanField, FormField, choice set, pycamp2009
Cc: Nicolás Miyasato, fperetti, Sebastian Noack Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

When you give a BooleanField a choice set, a default, and blank=False, the corresponding formfield still offers a blank '---------' option and does not initially set the default value.

Attachments (1)

patch.diff (554 bytes ) - added by fperetti 16 years ago.
New patch, now must work!

Download all attachments as: .zip

Change History (17)

comment:1 by Jacob, 16 years ago

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:2 by fperetti, 16 years ago

Owner: changed from nobody to fperetti

comment:3 by fperetti, 16 years ago

I cant reproduce the bug, someone can do it?

in reply to:  3 comment:4 by Nicolás Miyasato, 16 years ago

Hi, I've been working with fperetti and could reproduce part of the issue.
AFAIK the issue should be splitted in two parts:

  1. When the blank = True is passed to the models.BooleanField, the generated HTML (using ModelForm) is not consistent with that, as it adds the "------" option.
  2. When the default = N is passed to the models.BooleanField, the generated HTML does not have the default = "default"

We could only reproduce the first one using this:

#models.py

from django.db import models

# Create your models here.

CHOICE = [(1,'si'),(2,'no')]
class Ejemplo(models.Model):
       buleanito = models.BooleanField(choices=CHOICE,default=1, blank=False)

# forms.py
from django.forms import ModelForm
from coso.models import Ejemplo

class MiFormulario(ModelForm):
    class Meta:
        model = Ejemplo

./ḿanage shell

from coso.forms import MiFormulario
a = MiFormulario()
print(a)

<tr><th><label for="id_buleanito">Buleanito:</label></th><td><select name="buleanito" id="id_buleanito">

<option value="">---------</option>   # <-------------------------- THIS IS WRONG!!!

<option value="1" selected="selected">si</option>

<option value="2">no</option>

</select></td></tr>

comment:5 by Nicolás Miyasato, 16 years ago

Cc: Nicolás Miyasato added

comment:6 by fperetti, 16 years ago

Cc: fede added

comment:7 by fperetti, 16 years ago

Cc: fperetti added; fede removed

comment:8 by fperetti, 16 years ago

Resolution: fixed
Status: newclosed
  • django/db/models/fields/__init__.py

     
    371371
    372372class BooleanField(Field):
    373373    def __init__(self, *args, **kwargs):
    374         kwargs['blank'] = True
     374        kwargs['blank'] = False
    375375        if 'default' not in kwargs and not kwargs.get('null'):
    376376            kwargs['default'] = False
    377377        Field.__init__(self, *args, **kwargs)
Last edited 9 years ago by Tim Graham (previous) (diff)

comment:9 by fperetti, 16 years ago

Resolution: fixed
Status: closedreopened

comment:10 by Nicolás Miyasato, 16 years ago

So we found that the BooleanField class forces us to have the blank value to True

 Index: django/db/models/fields/_init_.py
===================================================================
--- django/db/models/fields/_init_.py (revisión: 10119)
+++ django/db/models/fields/_init_.py (copia de trabajo)
@@ -371,7 +371,7 @@

class BooleanField(Field):
    def __init__(self, *args, **kwargs):
-        kwargs['blank'] = True  # <------------- This is always set to True!!!
+        kwargs['blank'] = False
        if 'default' not in kwargs and not kwargs.get('null'):
            kwargs['default'] = False
        Field.__init__(self, *args, **kwargs)

As kwargs[blank] is set always to True, it doesn't matter what we pass to the BooleanField.

Does anybody know why this is set always to True?

I can't have a select/combobox with only two options, neither of them being the "--------" one, as it always adds it.

A patch has been submitted, but please tell us why this is hardcoded to the True value.
With this patch, sub-issue *1*

Thanks a lot

comment:11 by fperetti, 16 years ago

Has patch: set

by fperetti, 16 years ago

Attachment: patch.diff added

New patch, now must work!

comment:12 by fperetti, 16 years ago

Keywords: pycamp2009 added

comment:13 by Ramiro Morales, 16 years ago

See also #10549

comment:14 by Sebastian Noack, 16 years ago

Cc: Sebastian Noack added
Patch needs improvement: set

The blank flag in the BooleanField works in an other way as with most other Fields, since it is passed as required flag to the formfield and the default formfield for BooleanField renders a checkbox, which must be checked when required. I think my patch as submitted by the ticket #10549 would be a better solution.

comment:15 by Jacob, 16 years ago

Resolution: fixed
Status: reopenedclosed

(In [10500]) Fixed #9640, #10549: BooleanFields with choices, a default, and null=False now correctly doesn't generate a blank option.

comment:16 by Jacob, 13 years ago

milestone: 1.1

Milestone 1.1 deleted

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