4

我不知道为什么不起作用,但这是一个非常简单的例子

模型.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)

表格.py

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

网址.py

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

pytnon manage.py syncdb

在管理截图中

在网站上

为什么会这样?多次从基础模型中删除并再次创建

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布尔非空

:(

4

2 回答 2

2

模型.py

from django.core.exceptions import ValidationError

    def clean(self):
        if not self.personal:
            raise ValidationError('blah')

或者您可以在表单中验证它...

于 2012-11-18T13:01:16.637 回答
1

“必需”确实有效 - 它是一个 BooleanField,默认为False且不为空。
未选中它是一个有效值 (False)。

您可以覆盖 clean() 方法以确保它设置为 true。例如:

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

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

或以下列形式验证它:

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
于 2012-11-18T13:03:03.663 回答