模型中Django dateTime的正确格式是什么?

时间:2019-04-24 10:19:01

标签: python django postgresql pycharm

我是django的新手,正在创建我的第一个项目。我创建了一个应用程序,在该应用程序中,模型定义了一个postgres datatime字段,但是在运行migration命令时,该字段总是出错。

我在models.py中的日期时间字段使用了低于值的值,但是什么也没发生

created=models.DateTimeField(default=timezone.now)
created=models.DateTimeField(default='', blank=True, null=True)
created=models.DateTimeField(blank=True, null=True)
created=models.DateTimeField(default='0000-00-00 00:00:00', blank=True, null=True)

在我的settings.py

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

我的models.py

from django.db import models
from django.utils import timezone
# from django.contrib.auth.models import User

# Create your models here.
class Operator(models.Model):
operator_code=models.CharField(unique=True, max_length=20)
operator_name=models.CharField(max_length=60)
operator_type=models.CharField(max_length=60)
short_code=models.CharField(max_length=60)
apiworld_code=models.CharField(max_length=20)
apiworld_operatortype=models.CharField(max_length=20)
active_api_id=models.IntegerField(default=1)
ospl_commission=models.DecimalField(default=0.00, max_digits=11, decimal_places=2)
apiworld_commission=models.DecimalField(default=0.00, max_digits=11, decimal_places=2)
image=models.CharField(max_length=100)
status=models.SmallIntegerField(default=1)
updated=models.DateTimeField(default=timezone.now)
created=models.DateTimeField(default=timezone.now)

def __str__(self):
    return self.operator_code

当我运行“ python manage.py migration”时,出现以下错误如何删除此错误

'django.core.exceptions.ValidationError:[“'0000-00-00 00:00:00'值具有正确的格式(YYYY-MM-DD HH:MM [:ss [.uuuuuu]] [TZ ]),但它是无效的日期/时间。“]'

2 个答案:

答案 0 :(得分:3)

要回答您的问题,请在默认设置中定义Django中DateTimeField的格式,如此处所述:https://docs.djangoproject.com/en/2.2/ref/settings/#datetime-input-formats

目前,它默认为以下值,尽管显然会因您使用的Django版本而有所不同:

[
    '%Y-%m-%d %H:%M:%S',     # '2006-10-25 14:30:59'
    '%Y-%m-%d %H:%M:%S.%f',  # '2006-10-25 14:30:59.000200'
    '%Y-%m-%d %H:%M',        # '2006-10-25 14:30'
    '%Y-%m-%d',              # '2006-10-25'
    '%m/%d/%Y %H:%M:%S',     # '10/25/2006 14:30:59'
    '%m/%d/%Y %H:%M:%S.%f',  # '10/25/2006 14:30:59.000200'
    '%m/%d/%Y %H:%M',        # '10/25/2006 14:30'
    '%m/%d/%Y',              # '10/25/2006'
    '%m/%d/%y %H:%M:%S',     # '10/25/06 14:30:59'
    '%m/%d/%y %H:%M:%S.%f',  # '10/25/06 14:30:59.000200'
    '%m/%d/%y %H:%M',        # '10/25/06 14:30'
    '%m/%d/%y',              # '10/25/06'
]

但是,查看您的代码,我想您想知道答案“什么是在Django中创建默认DateTime值的正确方法。”

答案取决于您希望该默认值是什么。如果您希望该值为null,那么很简单。

myfield = models.DateTimeField(null=True, blank=True, default=None)

如果您尝试创建created_atupdated_at字段,那么您想分别使用auto_now_addauto_now kwarg,它们将自动使用创建和更新模型实例的时间。

created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

答案 1 :(得分:1)

我更新模型时遇到相同的格式错误。

我向您展示我的场景,也许会有所帮助。 所以我有一个现有的课程,想添加这一行:

time = models.TimeField()

比我做移民更重要的部分

Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value
for this column)
2) Quit, and let me add a default in models.py

选择1),然后键入timezone.now以设置默认值

迁移就知道了