简体   繁体   中英

Django DateField cannot be None

I'm really struggeling wih the DateTime cannot be None Issue of Django (3.0.4).

I Know this is a known Issue on Stackoverflow but I'm not able to solve it.

I do have the Field:

begin_holidays = models.DateField(null=True, blank=True)

And now if a submit a form where request.POST['begin_holidays'] is not set I get the error:

„“ is invalid date format. It has to be YYYY-MM-DD

Has anyone an Idea ?

If you leave it blank, it will submit the empty string, hence that will not work.

You can try to assign this:

myobject.begin_holidays = request.POST['begin_holidays'] 

But I strongly advise to make use of modelforms [Django-doc] instead. Since this will automatically do the cleaning, validation, and remove a lot of boilerplate code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM