Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在表单中添加日期_Python_Django - Fatal编程技术网

Python 在表单中添加日期

Python 在表单中添加日期,python,django,Python,Django,我有一个模型: class Post(models.Model): thread = model.ForeignKey(Thread) title = models.CharField(max_length=50, blank=True) text = models.TextField() author = models.CharField(max_length=20, default='Anonymous') date = models.DateTime

我有一个模型:

class Post(models.Model):
    thread = model.ForeignKey(Thread)
    title = models.CharField(max_length=50, blank=True)
    text = models.TextField()
    author = models.CharField(max_length=20, default='Anonymous')
    date = models.DateTimeField()
我有以下表格:

class PostForm(ModelForm):
    class Meta:
        model = models.Post
        fields = ['title', 'author', 'text']

我在表单中不使用日期字段。发送表单时如何添加当前日期?

您可以将日期字段的声明更改为:

date = models.DateTimeField(auto_now=True)

这将使日期字段在每次插入或更新时更改为“现在”。如果您只想在插入中使用,可以将其更改为
auto\u now\u add=True
。有关更多详细信息,请参阅。

您可以将日期字段的声明更改为:

date = models.DateTimeField(auto_now=True)

这将使日期字段在每次插入或更新时更改为“现在”。如果您只想在插入中使用,可以将其更改为
auto\u now\u add=True
。有关更多详细信息,请参阅。

@K1m现有数据库中是否有
Post.date==None
的记录?测试它:
SELECT*在日期为空的帖子中
@AdamSmith No。实际上,我的帖子有外键,我想它仍然是空的。@K1m您在现有数据库中是否有
Post.date==None的记录
?测试它:
SELECT*在日期为空的帖子中
@AdamSmith No。实际上,我的帖子有外键,我想它仍然是空的。