我有以下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”是否有任何问题。我怎么解决这个问题?我是新手。我已经发布了相关代码。
答案 0 :(得分:6)
将字段设置为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