How to make Django's DateTimeField optional?
I am trying to implement a to-do-list website to practice using Django. In models.py, I have a class called Item
to represent a to-do item. In it, I have the following line:
due_date = models.DateTimeField(required=False)
due_date
is meant to be an optional field in case the user has a deadline for some to-do item. The problem is that the line above gives me a TypeError
due to unexpected keyword argument 'required'.
So, it seems that I cannot use the keyword argument 'required' for DateTimeField
. Is there any way I can make a DateTimeField
optional? Or is there a standard implementation for the problem I am having?
Solution 1:
"required" is a valid argument for Django forms. For models, you want the keyword args blank=True
(for the admin) and null=True
(for the database).
Solution 2:
Use
due_date = models.DateTimeField(null=True, blank=True)
Check Field Options for more information.