关于python:我无法在django模型中添加datetime字段

I cannot add a datetime field in django model

型号.py

1
2
3
4
5
class user_Roles(models.Model):
   code = models.CharField(max_length=20)
   description = models.CharField(max_length=30)
   permitted_menus = models.CharField(max_length=200)
   created = models.DateTimeField(auto_now_add=True,blank=True,null=True)

我不能在模型中添加日期时间、日期或时间字段。它显示"[U""值的格式无效。迁移数据库时,它必须采用hh:mm[:ss[.uuuuuuuu]]格式。"]"。如何修复??

误差

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Running migrations:
  Rendering model states... DONE
  Applying myapp.0025_auto_20160321_0913...Traceback (most recent call last):
  File"./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File"/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File"/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File"/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File"/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File"/usr/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File"/usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File"/usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
    state = migration.apply(state, schema_editor)
  File"/usr/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File"/usr/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File"/usr/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 484, in alter_field
    old_db_params, new_db_params, strict)
  File"/usr/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 567, in _alter_field
    new_default = self.effective_default(new_field)
  File"/usr/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 211, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File"/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save
    prepared=False)
  File"/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 2293, in get_db_prep_value
    value = self.get_prep_value(value)
  File"/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 2288, in get_prep_value
    return self.to_python(value)
  File"/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 2275, in to_python
    params={'value': value},
django.core.exceptions.ValidationError: [u"'' value has an invalid format. It must be in HH:MM[:ss[.uuuuuu]] format."]

删除创建的字段后,也会显示相同的错误。


删除迁移文件夹。然后执行

1
2
python manage.py makemigrations
python manage.py migrate


您可以这样修改创建的字段

created = models.DateTimeField(default=date.today, blank=True,null=True)

检查自动添加文档