Python Question

Django ModelForm booleanfield Required field is not working

I do not know why does not work but it is very simple example

models.py

class Reg(models.Model):
event = models.ForeignKey(Event, verbose_name=u'Мероприятие')
#первый автор
fam_author = models.CharField(max_length=200, verbose_name=u'Фамилия автора' )
name_author = models.CharField(max_length=100, verbose_name=u'Имя автора' )
ot_author = models.CharField(max_length=200,blank=True, null=True, verbose_name=u'Отчество автора' )
no_ot = models.BooleanField(blank=True,verbose_name=u'Для граждан, не имеющих отчество по паспорту, отметить галочкой следующее поле')
...................
personal = models.BooleanField(verbose_name=u'Согласие на обработку и использование персональных данных')

class Meta:
verbose_name = 'Автор'
verbose_name_plural = 'Автор'

class Admin:
pass

def __unicode__(self):
return u'%s (%s)' % (self.fam_author, self.name_author)


forms.py

class GenForm11(ModelForm):
class Meta:
model = Reg
exclude = ('event')


urls.py

(r'^kras/', ContactWizard.as_view([GenForm11, GenForm31, GenForms4, GenForms5, GenForms6, GenForms7])),


pytnon manage.py syncdb

in admin sreenshot



in website


Why could this be? several times removed from the base model and created again

UPD1

python manage.py sqlall

CREATE TABLE `reg_reg` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`event_id` integer NOT NULL,
`fam_author` varchar(200) NOT NULL,
`name_author` varchar(100) NOT NULL,
`ot_author` varchar(200),
`no_ot` bool NOT NULL,
`birth_author` date NOT NULL,
`tel_m_author` varchar(15) NOT NULL,
`tel_r_author` varchar(15),
`tel_d_author` varchar(15),
`email_author` varchar(75) NOT NULL,
`email_d_author` varchar(75),
`text_author` longtext,
`facultet_diplom` varchar(50),
`napravl_diplom` varchar(50),
`kurs_diplom` varchar(50),
`group_diplom` varchar(50),
`text_diplom` longtext,
`post_diplom` varchar(200),
`sec` varchar(200) NOT NULL,
`personal` bool NOT NULL
)
;


personal
bool NOT NULL

:(

Answer

"required" does work - it's a BooleanField which defaults to False and not null.
Having it unchecked is a valid value (False).

You can override the clean() method to make sure it's set to true. For example:

class Reg(models.Model):
    # ...

    def clean(self):
        from django.core.exceptions import ValidationError
        if not self.personal:
            raise ValidationError('CUSTOM ERROR HERE')

Or validate it in the form:

class GenForm11(ModelForm):
    class Meta:
        model = Reg
        exclude = ('event')

    def clean_personal(self):
        personal = self.cleaned_data['personal']

        if not personal:
            raise ValidationError('CUSTOM ERROR HERE')

        return personal
Comments