How to make Django's DateTimeField optional?


Question

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?

1
55
7/5/2012 7:55:24 PM

Accepted Answer

"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).

89
7/5/2012 7:59:48 PM

Use due_date = models.DateTimeField(null=True, blank=True)

Check Field Options for more information.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon