添加Wagtail对象后,Django测试开始失败

时间:2018-05-16 06:51:41

标签: python django wagtail django-testing

我使用

为我的Django模型使用常规Django测试
./manage.py test non_wagtail_app_name

但突然间(加上一些Wagtail模型之后),我无法运行它们:

Creating test database for alias 'default'...
Traceback (most recent call last):
  File "./manage.py", line 12, in <module>
    execute_from_command_line(sys.argv)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/core/management/commands/test.py", line 29, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/core/management/commands/test.py", line 62, in handle
    failures = test_runner.run_tests(test_labels)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/test/runner.py", line 601, in run_tests
    old_config = self.setup_databases()
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/test/runner.py", line 546, in setup_databases
    self.parallel, **kwargs
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/test/utils.py", line 187, in setup_databases
    serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/db/backends/base/creation.py", line 69, in create_test_db
    run_syncdb=True,
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/core/management/__init__.py", line 131, in call_command
    return command.execute(*args, **defaults)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 88, in database_forwards
    field,
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 431, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 171, in column_sql
    default_value = self.effective_default(field)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 245, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 770, in get_db_prep_save
    prepared=False)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 762, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/wagtail/core/fields.py", line 106, in get_prep_value
    return json.dumps(self.stream_block.get_prep_value(value), cls=DjangoJSONEncoder)
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/wagtail/core/blocks/stream_block.py", line 252, in get_prep_value
    for child in value  # child is a StreamChild instance
  File "/home/fleon/development/virmyasb/lib/python3.6/site-packages/wagtail/core/blocks/stream_block.py", line 252, in <listcomp>
    for child in value  # child is a StreamChild instance
AttributeError: 'str' object has no attribute 'block'

回溯似乎来自我的项目的Wagtail部分(我的Wagtail模型使用StreamFields和BlockFields),所以我似乎错过了一些东西(为我的Wagtail对象放置固定装置),但是我是无从何从哪里开始(我还没有设置Wagtail测试 - 是的,我知道,我应该)。我想测试的应用程序并没有使用Wagtail中的任何内容。

唯一可行的是pg_dumping我的Postgres数据库,在生成的文件中编辑数据库名称,将其导入test_postgres并将-k开关传递给manage.py test,所以我需要很明显预制Wagtail表。但是,在Django模型测试中使用灯具并不能解决问题...

1 个答案:

答案 0 :(得分:1)

运行迁移时失败。挖掘堆栈跟踪,我最好的猜测是:

  • 这是一个向现有网页模型添加StreamField的迁移,作为必填(null=False)字段
  • 存在该页面模型的现有实例(很可能是它的HomePage,因为标准的Wagtail项目模板会在初始迁移中创建其中一个),因此需要使用默认值填充
  • 创建迁移时指定的默认值是非空字符串,它不是StreamField的有效值

如果是这样,修复方法是编辑违规迁移并将StreamField上的default值更改为空字符串。