DateTime字段显示无效的格式错误,同时通过窗体传递空值

时间:2018-08-02 05:45:23

标签: django django-models django-forms

我尝试提交表单,同时将datetime的字段保留为空,这会导致错误,显示datetime的格式无效。

我的模态

class lead(models.Model):
    assigned_to=models.CharField(max_length=15)
    ref=models.CharField(max_length=10,blank=True)
    lead_type=models.CharField(max_length=20)
    priority=models.CharField(max_length=20)
    hot=models.CharField(max_length=20,blank=True)
    source=models.CharField(max_length=20,blank=True)
    mls_lead=models.CharField(max_length=20,blank=True)
    auto_imported=models.CharField(max_length=20,blank=True)
    inquiry_date=models.DateTimeField(blank=True,null=True)
    lead_closed_date=models.DateTimeField(blank=True,null=True)
    created_by=models.CharField(max_length=20,blank=True)
    property_id=models.IntegerField(blank=True,null=True)
    contact_id=models.IntegerField(blank=True,null=True)

很明显,我为datetime字段和表单提供了null tr​​ue 我有  我的表格

class leads_form(forms.Form):
    assigned_to = forms.ChoiceField(choices=[(o.id, str(o.username)) for o in CustomUser.objects.all()],required=True,widget=forms.Select(attrs={'class':'form-control'}))
    ref = forms.CharField(max_length=50,required=False,widget=forms.TextInput(attrs={'class':'form-control'}))
    lead_type = forms.ChoiceField(choices=dicto.lead_type_dict.items(),required=True,widget=forms.Select(attrs={'class':'form-control slct'}))
    priority = forms.ChoiceField(choices=dicto.priority_dict.items(),required=True,widget=forms.Select(attrs={'class':'form-control slct'}))
    hot = forms.ChoiceField(choices=dicto.hot_dict.items(),required=True,widget=forms.Select(attrs={'class':'form-control slct'}))
    source = forms.ChoiceField(choices=dicto.source_dict.items(),required=True,widget=forms.Select(attrs={'class':'form-control slct'}))
    mls_lead = forms.ChoiceField(choices=dicto.true_false_dict.items(),required=True,widget=forms.Select(attrs={'class':'form-control slct'}))
    auto_imported = forms.ChoiceField(choices=dicto.true_false_dict.items(),required=False,widget=forms.Select(attrs={'class':'form-control slct'}))
    inquiry_date = forms.CharField(max_length=50,required=False,widget=forms.TextInput(attrs={'class':'form-control'}))
    lead_closed_date = forms.CharField(max_length=50,required=False,widget=forms.TextInput(attrs={'class':'form-control'}))
    created_by = forms.CharField(max_length=50,required=False,widget=forms.TextInput(attrs={'class':'form-control'}))
    property_id =  forms.IntegerField(required=False,widget=forms.HiddenInput())
    set_status = forms.ChoiceField(choices=dicto.set_status_dict.items(),required=True,widget=forms.Select(attrs={'class':'form-control slct','placeholder':'set status','required':'true'}))
    contact=forms.IntegerField(required=True,widget=forms.HiddenInput(attrs={'required':'true'}))

当我尝试使用验证表单数据创建模型对象时显示

django.core.exceptions.ValidationError: ["'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

我创建对象的方式如下

if form.is_valid():
            lead.objects.create(
                assigned_to=form.cleaned_data['assigned_to'],
                ref=form.cleaned_data['ref'],
                lead_type=form.cleaned_data['lead_type'],
                priority=form.cleaned_data['priority'],
                hot=form.cleaned_data['hot'],
                source=form.cleaned_data['source'],
                mls_lead=form.cleaned_data['mls_lead'],
                auto_imported=form.cleaned_data['auto_imported'],
                inquiry_date=form.cleaned_data['inquiry_date'],
                lead_closed_date=form.cleaned_data['lead_closed_date'],
                created_by=form.cleaned_data['created_by'],
                property_id=form.cleaned_data['property_id'],
                contact_id=form.cleaned_data['contact'])

1 个答案:

答案 0 :(得分:1)

对于Django的DateTimeField,空白字符串与None不同(或为null)。我认为这种混淆来自对空白字符串和None的真值的类似评估。

要断言没有引发错误,并且在需要时使用空值填充该字段,请对DateTimeField这样填充的行进行重做(通过lead.objects.create调用):

lead_closed_date=form.cleaned_data['lead_closed_date']

对此:

lead_closed_date=(
    form.cleaned_data['lead_closed_date'] 
    if form.cleaned_data['lead_closed_date'] 
    else None
)

使用此更正后,如果lead_closed_date真相值为form.cleaned_date['lead_closed_date'],它将用form.cleaned_data['lead_closed_date']填充True的值。