Code

Opened 5 years ago

Closed 5 years ago

Last modified 3 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: nmiyasato, fperetti, sebastian_noack Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:

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 5 years ago.
New patch, now must work!

Download all attachments as: .zip

Change History (17)

comment:1 Changed 5 years ago by jacob

  • milestone set to 1.1
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 5 years ago by fperetti

  • Owner changed from nobody to fperetti

comment:3 follow-up: Changed 5 years ago by fperetti

I cant reproduce the bug, someone can do it?

comment:4 in reply to: ↑ 3 Changed 5 years ago by nmiyasato

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 Changed 5 years ago by nmiyasato

  • Cc nmiyasato added

comment:6 Changed 5 years ago by fperetti

  • Cc fede added

comment:7 Changed 5 years ago by fperetti

  • Cc fperetti added; fede removed

comment:8 Changed 5 years ago by fperetti

  • Resolution set to fixed
  • Status changed from new to 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? = False

if 'default' not in kwargs and not kwargs.get('null'):

kwargsdefault? = False

Field.init(self, *args, kwargs)

comment:9 Changed 5 years ago by fperetti

  • Resolution fixed deleted
  • Status changed from closed to reopened

comment:10 Changed 5 years ago by nmiyasato

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 5 years ago by fperetti

  • Has patch set

Changed 5 years ago by fperetti

New patch, now must work!

comment:12 Changed 5 years ago by fperetti

  • Keywords set, pycamp2009 added; set removed

comment:13 Changed 5 years ago by ramiro

See also #10549

comment:14 Changed 5 years ago by sebastian_noack

  • 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 5 years ago by jacob

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

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

comment:16 Changed 3 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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.