3

我有以下 models.py 代码:

class try(models.Model)
    date = models.CharField(max_length=10, blank=True, null=True) # statement no. 1
    #also tried this instead of statement no.1 :
    #date = models.DateField(blank=True, null=True)
    #other statements

对应的ModelForm代码为:

class MyForm(ModelForm):
    filing_date = DateField(input_formats=['%d-%m-%Y'], widget=DateInput(format=('%d-%m-%Y'), attrs={'class':'input-block-level datePickBootstrap', 'placeholder':'Filing date'}))
    class Meta:
        model = try

如果“日期”字段留空,我收到错误“此字段为必填项”。考虑到我使用了“blank = True”,我无法找出错误的原因。我还尝试搜索“DateField”是否有任何问题。我怎么解决这个问题?我是新手。我已经发布了相关代码。

4

1 回答 1

8

将字段设置为required=False

class MyForm(ModelForm):
    filing_date = DateField(input_formats=['%d-%m-%Y'],
                            widget=DateInput(format=('%d-%m-%Y'), attrs={'class':'input-block-level datePickBootstrap', 'placeholder':'Filing date'}),
                            required=False)
    class Meta:
        model = try
于 2013-06-21T12:05:46.097 回答