On 10/31/05, Emanuele <[EMAIL PROTECTED]> 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=[is_not_null])

Adrian

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

Reply via email to