[英]Django TypeError: expected string or buffer
将DateField添加到模型中时,有时会弹出一个问题。 我相信将日期字段添加到已经有数据的表中时会发生这种情况。问题是我无法使错误消失。 每次我运行迁移时,都会报错TypeError: expected string or buffer
有没有办法强制迁移。 甚至在我从模型中删除字段,删除数据库并删除迁移之后,都会发生这种情况。 我也尝试过手动将datestamp输入数据库。
我可以解决的唯一方法是创建一个新项目并复制代码。
我将创建和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
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
我使用时区模块,如果您的项目知道时区,则首选此模块。
如果@petkostas提供的解决方案不起作用,请尝试在模型中使用str而不是unicode
例:
def __str__(self):
return self.title
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.