当前位置: 代码迷 >> 综合 >> It seems you set a fixed date / time / datetime value as default for this field.
  详细解决方案

It seems you set a fixed date / time / datetime value as default for this field.

热度:85   发布时间:2023-12-17 06:44:37.0

异常描述:

WARNINGS:
projects.Projects.create_time: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
 

 

from django.db import models# Create your models here.from django.db import models
from django.utils import timezoneclass Projects(models.Model):name = models.CharField(max_length=100)create_time = models.DateTimeField(default=timezone.now)# create_time = models.DateTimeField(default=timezone.now())# 添加()报错update_time = models.DateTimeField(default=timezone.now)def __str__(self):return self.name

 

区别在于,timezone.now一个可调用对象在运行时执行,同时timezone.now()返回该函数的输出。

去掉()就可以了。

 

 

  相关解决方案