擅长:python、mysql、java
<p>在定义模型时,似乎<code>datetime.now()</code>正在计算,而不是每次添加记录时。</p>
<p>Django有一个功能可以完成您已经在尝试的工作:</p>
<pre><code>date = models.DateTimeField(auto_now_add=True, blank=True)
</code></pre>
<p>或者</p>
<pre><code>date = models.DateTimeField(default=datetime.now, blank=True)
</code></pre>
<p>第二个例子和你现在的例子的区别在于缺少括号。通过传递不带括号的<code>datetime.now</code>,您将传递实际函数,每次添加记录时都将调用该函数。如果您传递它<code>datetime.now()</code>,那么您只需计算函数并传递返回值。</p>
<p>更多信息请访问Django的<a href="http://docs.djangoproject.com/en/dev/ref/models/fields/#datetimefield" rel="noreferrer">model field reference</a></p>