Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » NullBooleanField & default=Unknown & Unknown not acceptable: how to do it? [RSS Feed]

#1 Oct. 28, 2005 15:18:19

E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

NullBooleanField & default=Unknown & Unknown not acceptable: how to do it?


Hi,
using django automatic admin interface I need to show a select-box with
"True", "False" and "Unknown" values for a certain field of an object.
NullBooleanField is the natural choice as type of that field, but I
want to add a constraint and a default value in this way:
- default value should be "Unknown"
- "Unknown" value has not to be accepted as a valid value for that
field by the admin interface

The idea is to force the user to explicitly choose among Yes and No
without being able to skip that choice. Is there a better way to do it?

As far as I understand if I don't specify "default=<something>" the
default value is "Unknown" for NullBooleanField, as it is required. But
is it possible to say it explicitly in some way? default="Unknown" of
default=None don't work.

About the second point (avoiding "Unknown" value as an admin interface
constraint) I have no ideas.

Hints?

Thanks in advance,

Emanuele

Offline

#2 Oct. 31, 2005 14:55:38

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

NullBooleanField & default=Unknown & Unknown not acceptable: how to do it?


On 10/28/05, Emanuele <> wrote:
> using django automatic admin interface I need to show a select-box with
> "True", "False" and "Unknown" values for a certain field of an object.
> NullBooleanField is the natural choice as type of that field, but I
> want to add a constraint and a default value in this way:
> - default value should be "Unknown"
> - "Unknown" value has not to be accepted as a valid value for that
> field by the admin interface

If "Unknown" values are not allowed, it seems like you should be using
BooleanField instead of NullBooleanField.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

#3 Oct. 31, 2005 21:20:44

E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

NullBooleanField & default=Unknown & Unknown not acceptable: how to do it?


OK, but using "Unknown" was just a trick. The real problem is: how to
_force_ the user to _choose_ between Yes and No avoiding a default
value? A BooleanField shows a checkbox in admin interface: if you don't
specify a default value and if you don't click on the box, it returns
'False'. So a default value is present ('False') even if you didn't
specify it. A checkbox can't avoid a default value.

Getting requirements for the application I'm writing people told me
that a particular field, that is boolean, is really important and who
enters data should be forced to explicitly click on yes or no without
being able to save data before specifying the choice. Is it possible to
do it in django?

I believe that in this case I need a three-state choice ('Yes', 'No',
'---') with a default ('---') that can't be accepted by the application
(or admin interface to be precise), so the user is forced to
explicitely choose 'Yes' or 'No'. But how to do it?

Anyway thanks for the answer!

Emanuele

Offline

#4 Oct. 31, 2005 21:59:29

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

NullBooleanField & default=Unknown & Unknown not acceptable: how to do it?


On 10/31/05, Emanuele <> wrote:
> OK, but using "Unknown" was just a trick. The real problem is: how to
> _force_ the user to _choose_ between Yes and No avoiding a default
> value? A BooleanField shows a checkbox in admin interface: if you don't
> specify a default value and if you don't click on the box, it returns
> 'False'. So a default value is present ('False') even if you didn't
> specify it. A checkbox can't avoid a default value.
>
> Getting requirements for the application I'm writing people told me
> that a particular field, that is boolean, is really important and who
> enters data should be forced to explicitly click on yes or no without
> being able to save data before specifying the choice. Is it possible to
> do it in django?

Ah, I see. Yes, this is completely possible. Here are two ways to solve it:

1. Use "choices", like so:

meta.CharField(maxlength=1, choices=(('', '----'), ('T', 'True'),
('F', 'False')))

2. Use NullBooleanField with a custom validator, like so:

from django.core import validators
def is_not_null(field_data, all_data):
if field_data in (None, ''):
raise validators.ValidationError, "This field is required."
# ...
meta.NullBooleanField(validator_list=)

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

#5 Oct. 31, 2005 22:55:27

E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

NullBooleanField & default=Unknown & Unknown not acceptable: how to do it?


Wow! Thank you very much Adrian!

I still don't understand where solution 1 avoids '----' as a possible
choice by the user, but anyway it's time for me to study validators:
solution 2 seems perfect. Let's do it tomorrow, here it's late in the
night :)

Emanuele

Offline

#6 Oct. 31, 2005 23:10:21

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

NullBooleanField & default=Unknown & Unknown not acceptable: how to do it?


On 10/31/05, Emanuele <> wrote:
> I still don't understand where solution 1 avoids '----' as a possible
> choice by the user, but anyway it's time for me to study validators:
> solution 2 seems perfect. Let's do it tomorrow, here it's late in the
> night :)

Solution 1 avoids "----" as a possible choice because the field
doesn't have "blank=True". By default, "blank" is set to False, which
means the field is required (i.e. cannot have a blank value). Hope
that clears it up for ya!

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

#7 Nov. 1, 2005 08:58:54

E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

NullBooleanField & default=Unknown & Unknown not acceptable: how to do it?


Now I see. So "----" means blank value. I missed it in the docs. Thank
you again!!

Emanuele

Offline

#8 Nov. 1, 2005 10:13:43

James B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

NullBooleanField & default=Unknown & Unknown not acceptable: how to do it?


On 11/1/05, Emanuele <> wrote:
> Now I see. So "----" means blank value. I missed it in the docs. Thank
> you again!!

No, the example Adrian provided had the tuple ('', '---') for that
choice; '---' is what will show up in the admin, but '' is the actual
value it will try to insert. That's two single quotes with nothing in
between, which means the value it attempts to save would be an empty
string. So they'd get thrown back an error and told to select
something else, because the field doesn't allow blank values.


--
"May the forces of evil become confused on the way to your house."
-- George Carlin

Offline

#9 Nov. 1, 2005 14:19:10

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

NullBooleanField & default=Unknown & Unknown not acceptable: how to do it?


On 11/1/05, James Bennett <> wrote:
> On 11/1/05, Emanuele <> wrote:
> > Now I see. So "----" means blank value. I missed it in the docs. Thank
> > you again!!
>
> No, the example Adrian provided had the tuple ('', '---') for that
> choice; '---' is what will show up in the admin, but '' is the actual
> value it will try to insert. That's two single quotes with nothing in
> between, which means the value it attempts to save would be an empty
> string. So they'd get thrown back an error and told to select
> something else, because the field doesn't allow blank values.

Yes, what James said. :)

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

#10 Nov. 1, 2005 21:23:49

E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

NullBooleanField & default=Unknown & Unknown not acceptable: how to do it?


Ouch! Sorry for the confusion : maybe the forces of evil are close to
my house. ;)

Emanuele

Offline

  • Root
  • » Django
  • » NullBooleanField & default=Unknown & Unknown not acceptable: how to do it? [RSS Feed]

Board footer

Moderator control

Enjoy the 20th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support