当我向模型添加DateField时,有时会弹出一个问题。我相信当我将一个Date字段添加到已经包含数据的表时会发生这种情况。问题是我无法使错误消失。每次我运行迁移时都会出现错误TypeError: expected string or buffer
有没有办法强制迁移。它甚至发生在我从模型中删除字段,删除数据库和删除迁移之后。我还尝试手动输入数据库中的日期戳。
我可以解决的唯一方法是创建一个新项目并复制代码。
我将创建和last_modified添加到了错误的模型
class Entry(models.Model):
title = models.CharField(max_length=50, blank=True, null=True)
start = models.DateField(blank=True, null=True)
end = models.DateField(blank=True, null=True)
creation = models.DateField(auto_now_add=True)
last_modified = models.DateField(auto_now=True)
def __unicode__(self):
return self.title
我已经恢复到它工作的时候了。
class Entry(models.Model):
title = models.CharField(max_length=50, blank=True, null=True)
start = models.DateField(blank=True, null=True)
end = models.DateField(blank=True, null=True)
def __unicode__(self):
return self.title
完全错误:
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/User/pyProjects/projects/Folder/Env/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Users/User/pyProjects/projects/Folder/Env/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/User/pyProjects/projects/Folder/Env/lib/python2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/User/pyProjects/projects/Folder/Env/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/Users/User/pyProjects/projects/Folder/Env/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 "/Users/User/pyProjects/projects/Folder/Env/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 "/Users/User/pyProjects/projects/Folder/Env/lib/python2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/User/pyProjects/projects/Folder/Env/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 "/Users/User/pyProjects/projects/Folder/Env/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/Users/User/pyProjects/projects/Folder/Env/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 179, in add_field
self._remake_table(model, create_fields=[field])
File "/Users/User/pyProjects/projects/Folder/Env/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 77, in _remake_table
self.effective_default(field)
File "/Users/User/pyProjects/projects/Folder/Env/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 "/Users/User/pyProjects/projects/Folder/Env/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save
prepared=False)
File "/Users/User/pyProjects/projects/Folder/Env/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1322, in get_db_prep_value
value = self.get_prep_value(value)
File "/Users/User/pyProjects/projects/Folder/Env/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1317, in get_prep_value
return self.to_python(value)
File "/Users/User/pyProjects/projects/Folder/Env/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1274, in to_python
parsed = parse_date(value)
File "/Users/User/pyProjects/projects/Folder/Env/lib/python2.7/site-packages/django/utils/dateparse.py", line 60, in parse_date
match = date_re.match(value)
TypeError: expected string or buffer
答案 0 :(得分:0)
auto_now
和auto_now_add
过去曾导致迁移问题,最好的方法是使用默认字段。
from django.utils import timezone
class Entry(models.Model):
title = models.CharField(max_length=50, blank=True, null=True)
start = models.DateField(blank=True, null=True)
end = models.DateField(blank=True, null=True)
creation = models.DateField(default=timezone.now())
last_modified = models.DateField(default=timezone.now())
def __unicode__(self):
return self.title
我使用时区模块,如果你的项目是时区感知的话,这是首选。
答案 1 :(得分:0)
如果@petkostas的解决方案无效,请尝试使用 str 代替 unicode
示例:
def __str__(self):
return self.title