如何将当前时间设置为Django模型中“TimeField()”的默认值?

huangapple go评论66阅读模式
英文:

How to set the current time to "TimeField()" as a default value in Django Models?

问题

文档中在DateField.auto_now_add下面说:

> 当对象首次创建时,自动将字段设置为当前时间。...如果您想要能够修改此字段,请设置以下内容,而不是auto_now_add=True

  • 对于DateFielddefault=date.today - from datetime.date.today()
  • 对于DateTimeFielddefault=timezone.now - from django.utils.timezone.now()

因此,对于DateField()DateTimeField(),我可以分别将当前日期和时间设置为默认值,如下所示:

from datetime import date
from django.utils import timezone

class MyModel(models.Model):
    date = models.DateField(default=date.today) # 这里
    datetime = models.DateTimeField(default=timezone.now) # 这里

现在,如何将当前时间设置为TimeField()的默认值,如下所示:

class MyModel(models.Model):       # ?
    time = models.TimeField(default=...)
英文:

The doc says below in DateField.auto_now_add:

> Automatically set the field to now when the object is first created. ... If you want to be able to modify this field, set the following instead of auto_now_add=True:

  • For DateField: default=date.today - from datetime.date.today()
  • For DateTimeField: default=timezone.now - from django.utils.timezone.now()

So to DateField() and DateTimeField(), I can set the current date and time respectively as a default value as shown below:

from datetime import date
from django.utils import timezone

class MyModel(models.Model):
    date = models.DateField(default=date.today) # Here
    datetime = models.DateTimeField(default=timezone.now) # Here

Now, how can I set the current time to TimeField() as a default value as shown below?

class MyModel(models.Model):       # ?
    time = models.TimeField(default=...)

答案1

得分: 1

你可以使用一个可调用对象

from django.utils import timezone

def <b>current_time</b>():
    return timezone.now()<b>.time()</b>;

class MyModel(models.Model):
    time = models.TimeField(default=<b>current_time</b>)

每次你定义一个新的 MyModel,如果没有传递时间参数,它将调用 current_time 方法,因此返回当前(带时区的)时间的时间部分。

重要的是不要使用括号,所以 default=current_time,而不是 <s>default=current_time()`。

英文:

You can use a callable:

<pre><code>from django.utils import timezone

def <b>current_time</b>():
return timezone.now()<b>.time()</b>

class MyModel(models.Model):
time = models.TimeField(default=<b>current_time</b>)</code></pre>

each time you define a new MyModel and the time is not passed, it will call the current_time method and thus return the time component of the current (timezoned time).

It is important not to use parenthesis, so default=current_time, not <s>default=current_time()</s>.

答案2

得分: 0

你可以将current_time设置为没有()的形式,将timezone.now().time()作为TimeField()的默认值,如下所示。*不要使用()设置current_time(),因为默认时间是在Django服务器启动时确定的(不变):

from django.utils import timezone

def current_time(): # 这里
    return timezone.now().time()

class MyModel(models.Model): # 不要设置“current_time()”
    time = models.TimeField(default=current_time)

请注意,不要使用()timezone.now().time()设置为TimeField()的默认值,如下所示,因为默认时间是在Django服务器启动时确定的(不变):

from django.utils import timezone

class MyModel(models.Model): # 不要设置“timezone.now().time()”
    time = models.TimeField(default=timezone.now().time())

此外,不要将timezone.now().time设置为没有()的形式,作为TimeField()的默认值,如下所示,因为在运行python manage.py makemigrations时会出错:

from django.utils import timezone

class MyModel(models.Model): # 不要设置“timezone.now().time”
    time = models.TimeField(default=timezone.now().time)

另外,我不建议使用DateField()TimeField(),因为它们不与settings.py中的TIME_ZONE一起工作,正如我在提问时所询问的,而DateTimeField()可以。

英文:

You can set current_time without () which returns timezone.now().time() to TimeField() as a default value as shown below. *Don't set current_time() with () because the default time becomes when the Django server starts (Unchanged):

from django.utils import timezone

def current_time(): # Here
    return timezone.now().time()

class MyModel(models.Model): # Don&#39;t set &quot;current_time()&quot;
    time = models.TimeField(default=current_time)

Be careful, don't set timezone.now().time() with () to TimeField() as a default value as shown below because the default time becomes when the Django server starts (Unchanged):

from django.utils import timezone

class MyModel(models.Model): # Don&#39;t set &quot;timezone.now().time()&quot;
    time = models.TimeField(default=timezone.now().time())

And, don't set timezone.now().time without () to TimeField() as a default value as shown below because there is an error when running python manage.py makemigrations:

from django.utils import timezone

class MyModel(models.Model): # Don&#39;t set &quot;timezone.now().time&quot;
    time = models.TimeField(default=timezone.now().time)

In addition, I don't recommend to use DateField() and TimeField() because they don't work with TIME_ZONE in settings.py as I asked the question while DateTimeField() does.

huangapple
  • 本文由 发表于 2023年5月28日 19:47:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定