如何将NULL插入Django';是DateField吗?

如何将NULL插入Django';是DateField吗?,django,datepicker,datefield,Django,Datepicker,Datefield,我的django模型中有一个可选的日期字段: award_grant_date = models.DateField(null=True, blank=True) 当用户将日期字段留空时,将在日期字段中插入NULL,否则将插入日期 但是,如果用户确实在DateField中输入了日期,并且用户在表单中选择了一些其他特定数据,那么我想在DateField中插入NULL 因此,在我的表格中,我有以下代码: class AwardGrantDetailsForm(forms.Form):

我的django模型中有一个可选的日期字段:

award_grant_date = models.DateField(null=True, blank=True)
当用户将日期字段留空时,将在日期字段中插入NULL,否则将插入日期

但是,如果用户确实在DateField中输入了日期,并且用户在表单中选择了一些其他特定数据,那么我想在DateField中插入NULL

因此,在我的表格中,我有以下代码:

    class AwardGrantDetailsForm(forms.Form):
    ...
    award_grant_date = forms.DateField(
    widget=forms.DateInput(attrs={'id':'id_award_grant_date'}),
    input_formats=settings.DATE_INPUT_FORMATS,
    label=_('Date'),
    required=False)
    ...
    elif cd_agdf['award_grant_type'] == 8888 or cd_agdf['award_grant_type'] == 9999:

        self.cleaned_data['award_grant_type_description'] = ''
        self.cleaned_data['award_grant_date'] = 'NULL' //this is the issue here! 
我得到的错误是:

[u"'NULL' value has an invalid date format. It must be in YYYY-MM-DD format."]
我似乎无法获得正确的语法,无法在奖励授予日期字段中输入空值

我使用的是bootstrap datepicker和django 1.4


如何编写代码self.cleaned_data['award_grant_date']=''以插入所需的空值?我已经将models字段设置为null=True,blank=True

数据库null的Python表示为
None

self.cleaned_data['award_grant_date'] = None

NULL值的日期格式无效。它必须是YYYY-MM-DD格式

如果要将空值存储到模型中,请使用model\u field=None

解决方案:

registration_date = request.POST.get('registration_date')

if not registration_date:
    registration_date = None

#StudentInfo is my model
student = StudentInfo(name=request.POST.get('name'), std=request.POST.get('std'), registration_date=registration_date)

#Now you can save your model data
student.save()

谢谢实际上我试过使用None,但就像noob一样,我用引号括住了None。是的,这是Python中的一个特殊关键字。