如何使Django的DateTimeField可选? [英] How to make Django's DateTimeField optional?
问题描述
我正在尝试实现一个待办事项列表网站来练习使用Django。在models.py中,我有一个名为 Item
的类来表示待办事项。在其中,我有以下行:
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是一个可选字段,以防用户有一些待办事项的截止日期。问题是,上面的这行给我一个TypeError,因为意外的关键字参数'required'。
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'.
所以,似乎我不能使用关键字参数'required' DateTimeField字段。有什么办法可以使DateTimeField可选吗?还是有一个标准的实现我遇到的问题?
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?
推荐答案
必需是Django表单的有效参数。对于模型,您需要关键字args blank = True
(对于管理员)和 null = True
(对于数据库)。
"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).
这篇关于如何使Django的DateTimeField可选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!