3

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
Vabbs
  • 107
  • 14

1 Answers1

8

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
J. Ghyllebert
  • 2,009
  • 1
  • 29
  • 35
  • Thank you J. Ghyllebert. It worked. thanks a lot for your reply :) I want to Vote Up your answer, but i dont have enough reputation. Sorry for that – Vabbs Jun 21 '13 at 12:28
  • Glad to know that. Thank you once again – Vabbs Jun 21 '13 at 13:26