简体   繁体   English

Django TypeError:预期的字符串或缓冲区

[英]Django TypeError: expected string or buffer

I have an issue that sometimes pops up when I add a DateField to my models. 将DateField添加到模型中时,有时会弹出一个问题。 I believe it to occur when I add a Date field to a table that already has data in. The problem is that i can not make the error go away. 我相信将日期字段添加到已经有数据的表中时会发生这种情况。问题是我无法使错误消失。 Every time i run migrate it errors TypeError: expected string or buffer 每次我运行迁移时,都会报错TypeError: expected string or buffer

Is there a way to force the migrate. 有没有办法强制迁移。 It even happens after I have deleted the field from the model, deleted the database and deleted migrations. 甚至在我从模型中删除字段,删除数据库并删除迁移之后,都会发生这种情况。 I have also tried manually entering in the datestamp into the database. 我也尝试过手动将datestamp输入数据库。

The only way I can get around it is to create a new project and copying the code over. 我可以解决的唯一方法是创建一个新项目并复制代码。

I added creation and last_modified to the model that coursed the error 我将创建和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

I have since reverted back to when it was working. 从那以后,我又回到了它工作的时候。

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

Full Error: 完全错误:

  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 and auto_now_add have caused problems with migrations in the past, the best approach would be to use the default field instead. auto_nowauto_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

I use the timezone module, this is preferred if your project is timezone aware. 我使用时区模块,如果您的项目知道时区,则首选此模块。

If solution by @petkostas doesn't work try using str for your model instead of unicode 如果@petkostas提供的解决方案不起作用,请尝试在模型中使用str而不是unicode

example: 例:

def __str__(self):
        return self.title

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM