【问题标题】:How to get Current Date and Time in Django [duplicate]如何在 Django 中获取当前日期和时间 [重复]
【发布时间】:2020-04-26 19:56:24
【问题描述】:

无法弄清楚我在做什么错误

我知道这个问题已经被问过了,但仍然面临这些错误

from datetime import datetime

class Images(models.Model):
  created_date = models.IntegerField(default=datetime.now().date())
  created_time = models.IntegerField(default=datetime.now().time())
  road_name = models.CharField(max_length = 100, default = 'NULL')

上面的代码出错了

Applying images.0002_auto_20200109_0728...Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/migrate.py", line 234, in handle
    fake_initial=fake_initial,
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/operations/fields.py", line 249, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 535, in alter_field
    old_db_params, new_db_params, strict)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/postgresql/schema.py", line 122, in _alter_field
    new_db_params, strict,
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 648, in _alter_field
    old_default = self.effective_default(old_field)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 233, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 789, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 1429, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 1408, in get_prep_value
    value = super().get_prep_value(value)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 1268, in get_prep_value
    return self.to_python(value)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 1393, in to_python
    params={'value': value},
django.core.exceptions.ValidationError: ["'07:28:18' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

当我使用 Django 核心 DateTimeField 时,我常常会卡在 auto_now_add 和 auto_now 字段中

class Images(models.Model):
  created_date = models.DateTimeField(auto_now_add=True, default=timezone.now())
  created_time = models.TimeField(auto_now=True, default=timezone.now())

错误

SystemCheckError: System check identified some issues:

ERRORS:
images.PotholeImages.created_date: (fields.E160) The options auto_now, auto_now_add, and default are mutually exclusive. Only one of these options may be present.
images.PotholeImages.created_time: (fields.E160) The options auto_now, auto_now_add, and default are mutually exclusive. Only one of these options may be present.

谢谢

【问题讨论】:

  • 你为什么在这里使用IntegerField

标签: django django-models


【解决方案1】:

我强烈建议不要使用IntegerFields,但在这里使用DateTimeField [Django-doc]。您可以将True 传递给auto_now_add=… parameter [Django-doc] 以自动设置当前时间:

class MyModel(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    road_name = models.CharField(max_length=100)

这不仅会在您创建新的MyModel 记录时设置当前时间,还会使字段不可编辑(将editable=… parameter [Django-doc] 设置为False),并将blank=… parameter [Django-doc] 设置为@ 987654334@.

不要DateTimeField 拆分为两个单独的字段(DateFieldTimeField)。时间证明是“日期敏感的”。事实上,时区和夏令时意味着某些日期在时间上执行“转换”。

【讨论】:

  • 感谢您的快速回复,但 auto_now_add 仍然存在错误,请您查看一下
  • @PradyumGupta:但您应该删除 default=... paramete.r 这是由auto_now_add 处理的。
  • 我已经使用created_date = models.DateTimeField(auto_now_add=True) 一段时间了created_time = models.DateTimeField(auto_now_add=True) 仍然存在错误django.core.exceptions.ValidationError: ["'07:28:18' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
  • @PradyumGupta:使用两个字段没有多大意义。此外,您可能有一个额外的字段引发错误,或者没有迁移您的模型。
  • 谢谢你在迁移中有问题,不知何故我试图弄清楚我应该如何在两个不同的字段中只获得时间和日期
猜你喜欢
  • 2015-11-12
  • 2012-08-15
  • 2012-01-10
  • 2011-04-27
  • 2019-11-28
  • 2011-01-01
  • 1970-01-01
  • 2010-10-03
  • 2019-08-13
相关资源
最近更新 更多