user2137817
user2137817

Reputation: 1853

How to save an empty datefield in django

I have an optional datefield in my form, when I save it and it's empty I got the validation error about the invalid format. In my model, I wrote blank=True and null=True.

Model :

created = models.DateTimeField(blank=True,null=True)

Form :

class XxxForm(forms.ModelForm):

    created=forms.DateField(required=False)
    class Meta:
        model = Xxx

Error :

u"" value has an invalid format. It must be in YYYY-MM-DD HH:MM format

Update : I found the solution which is :

created = request.POST['created']
        if not created:
                created = None

It works that way ! thanks everyone

Upvotes: 2

Views: 1418

Answers (1)

RickyA
RickyA

Reputation: 16029

You need to use a DateTimeField in the form, not a DateField.

[EDIT] Also try to drop created=forms.DateField(required=False) from your form declaration. It is not needed there since you have it in the model already.

Upvotes: 1

Related Questions