【问题标题】:Why does using a django BooleanField in models.py cause a TypeError?为什么在 models.py 中使用 django BooleanField 会导致 TypeError?
【发布时间】:2020-04-10 15:58:02
【问题描述】:

我正在为待办事项列表程序创建一个模型。这是我的 models.py 文件:

from django.db import models
from django.contrib.auth.models import User

class Todo(models.Model):
    content = models.CharField(max_length=250)
    completed = models.BooleanField(default=False)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.content

当我在进行迁移后尝试运行 python manage.py migrate 时,它给了我这个错误:

TypeError: memoryview: a bytes-like object is required, not 'bool'

我不确定如何解决这个问题,但我猜它与布尔字段有关。提前致谢!

编辑: 这是我得到的全部错误:

File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/Users/thuitema/anaconda3/lib/python3.7/site- 
   packages/django/core/management/__init__.py", line 401, in 
    execute_from_command_line
    utility.execute()
  File "/Users/thuitema/anaconda3/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/thuitema/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/thuitema/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/Users/thuitema/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/thuitema/anaconda3/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 233, in handle
    fake_initial=fake_initial,
  File "/Users/thuitema/anaconda3/lib/python3.7/site-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 "/Users/thuitema/anaconda3/lib/python3.7/site-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 "/Users/thuitema/anaconda3/lib/python3.7/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/thuitema/anaconda3/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Users/thuitema/anaconda3/lib/python3.7/site-packages/django/db/migrations/operations/fields.py", line 112, in database_forwards
    field,
  File "/Users/thuitema/anaconda3/lib/python3.7/site-packages/django/db/backends/sqlite3/schema.py", line 328, in add_field
    self._remake_table(model, create_field=field)
  File "/Users/thuitema/anaconda3/lib/python3.7/site-packages/django/db/backends/sqlite3/schema.py", line 189, in _remake_table
    self.effective_default(create_field)
  File "/Users/thuitema/anaconda3/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 303, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "/Users/thuitema/anaconda3/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 821, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/Users/thuitema/anaconda3/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 2262, in get_db_prep_value
    return connection.Database.Binary(value)
TypeError: memoryview: a bytes-like object is required, not 'bool'

【问题讨论】:

  • 您能否向我们展示整个堆栈跟踪,而不仅仅是最后一行?
  • 我将整个错误添加到原始问题中。

标签: python django function class


【解决方案1】:
File "/Users/xxxxx/anaconda3/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 2262, in get_db_prep_value
    return connection.Database.Binary(value)
TypeError: memoryview: a bytes-like object is required, not 'bool'

错误日志的最后两行表明该模型最初是使用models.BinaryField() 字段类型构建的,这将引发default=False 的此类型错误。使用上面已有的 models.BooleanField(default=False) 行重建和运行迁移应该可以解决它:

python manage.py makemigrations
python manage.py migrate

【讨论】:

    猜你喜欢
    • 2020-02-03
    • 2017-04-02
    • 2016-09-03
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 2020-03-04
    • 2021-08-30
    • 1970-01-01
    相关资源
    最近更新 更多