Inspite of setting (null=True, blank=True) for ModelForm field, I get validation error on leaving the field blank

StackOverflow https://stackoverflow.com/questions/17234496

I have following models.py code:

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

Corresponding ModelForm code is:

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

I get the error "This field is required", if the "date" field is left blank. Considering that I have used "blank = True", I am unable to figure out the reason for the error. I also tried searching if there are any issues with "DateField". How can I solve this problem? I am a newbie. I have posted relevant code.

有帮助吗?

解决方案

Set the field to 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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top