#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)
Change History (17)
comment:1 Changed 12 years ago by
milestone: | → 1.1 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 Changed 12 years ago by
Owner: | changed from nobody to fperetti |
---|
comment:3 follow-up: 4 Changed 12 years ago by
comment:4 Changed 12 years ago by
Hi, I've been working with fperetti and could reproduce part of the issue.
AFAIK the issue should be splitted in two parts:
- 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.
- 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 Changed 12 years ago by
Cc: | Nicolás Miyasato added |
---|
comment:6 Changed 12 years ago by
Cc: | fede added |
---|
comment:7 Changed 12 years ago by
Cc: | fperetti added; fede removed |
---|
comment:8 Changed 12 years ago by
Resolution: | → fixed |
---|---|
Status: | new → closed |
===================================================================
--- 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):
- kwargsblank? = True
+ kwargsblank? = False
if 'default' not in kwargs and not kwargs.get('null'):
kwargsdefault? = False
Field.init(self, *args, kwargs)
comment:9 Changed 12 years ago by
Resolution: | fixed |
---|---|
Status: | closed → reopened |
comment:10 Changed 12 years ago by
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 Changed 12 years ago by
Has patch: | set |
---|
comment:12 Changed 12 years ago by
Keywords: | pycamp2009 added |
---|
comment:14 Changed 12 years ago by
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 Changed 12 years ago by
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
I cant reproduce the bug, someone can do it?