StreamField – TypeError: 'bool' object is not iterable
StreamField – TypeError: 'bool' object is not iterable
构建:Wagtail CMS(1.13.1) 和 python3.6 中的 Django(1.11)。
我正在尝试创建一个非常基本的 Streamfield 块 (CardBlock),但一直出现类型错误。它与文档中的示例非常相似,但我无法让它工作...
class CardBlock(StructBlock):
image = ImageChooserBlock()
heading = CharBlock(classname="full title")
caption = RichTextBlock()
class Meta:
icon = 'image'
class HomePage(Page):
intro = RichTextField(blank=True)
showcase_title = RichTextField(blank=True)
card = StreamField([('card', CardBlock())], default=True)
content_panels = Page.content_panels + [
FieldPanel('intro', classname="full"),
MultiFieldPanel([
FieldPanel('showcase_title'),
StreamFieldPanel('card'),
]),
]
django 正在尝试 "get_db_prep_value()"。因此,Wagtail 尝试 "get_prep_value()",对于 value 中的所有 children(streamchild 实例),如下所示。 wagtail/wagtailcore/blocks/stream_block.py(第 257 行):
def get_prep_value(self, value):
if value is None:
# treat None as identical to an empty stream
return []
return [
{
'type': child.block.name,
'value': child.block.get_prep_value(child.value),
# assign a new ID on save if it didn't have one already
'id': child.id or str(uuid.uuid4()),
}
for child in value # child is a StreamChild instance
]
我不确定这个值是什么。
我的块 类 中的哪些内容需要更改才能更正此 value 变量?
Edit1- 完整错误:
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, home, sessions,
taggit, wagtailadmin, wagtailcore, wagtaildocs, wagtailembeds, wagtailforms,
wagtailimages, wagtailredirects, wagtailsearch, wagtailusers
Running migrations:
Applying home.0006_auto_20180220_1223...Traceback (most recent call last):
File "manage.py", line 12, in <module>
execute_from_command_line(sys.argv)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/core/management/__init__.py", line 363, in
execute_from_command_line
utility.execute()
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "//anaconda/envs/WagtailCMS/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 "//anaconda/envs/WagtailCMS/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 "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "//anaconda/envs/WagtailCMS/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 "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/db/migrations/operations/fields.py", line 86, in
database_forwards
field,
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/db/backends/sqlite3/schema.py", line 238, in add_field
self._remake_table(model, create_field=field)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/db/backends/sqlite3/schema.py", line 113, in _remake_table
self.effective_default(create_field)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/db/backends/base/schema.py", line 228, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/db/models/fields/__init__.py", line 766, in get_db_prep_save
prepared=False)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/db/models/fields/__init__.py", line 758, in get_db_prep_value
value = self.get_prep_value(value)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/wagtail/wagtailcore/fields.py", line 109, in get_prep_value
return json.dumps(self.stream_block.get_prep_value(value),
cls=DjangoJSONEncoder)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/wagtail/wagtailcore/blocks/stream_block.py", line 257, in
get_prep_value
for child in value # child is a StreamChild instance
TypeError: 'bool' object is not iterable
错误在线:
card = StreamField([('card', CardBlock())], default=True)
default
参数用于为字段指定一个default/initial值;在这种情况下,您将该值设置为 True
,这作为 StreamField 值没有意义。 (StreamField 是一个块列表,因此它正在尝试循环遍历值 True
来填充流...)
也许您的意思是 blank=False
?
构建:Wagtail CMS(1.13.1) 和 python3.6 中的 Django(1.11)。
我正在尝试创建一个非常基本的 Streamfield 块 (CardBlock),但一直出现类型错误。它与文档中的示例非常相似,但我无法让它工作...
class CardBlock(StructBlock):
image = ImageChooserBlock()
heading = CharBlock(classname="full title")
caption = RichTextBlock()
class Meta:
icon = 'image'
class HomePage(Page):
intro = RichTextField(blank=True)
showcase_title = RichTextField(blank=True)
card = StreamField([('card', CardBlock())], default=True)
content_panels = Page.content_panels + [
FieldPanel('intro', classname="full"),
MultiFieldPanel([
FieldPanel('showcase_title'),
StreamFieldPanel('card'),
]),
]
django 正在尝试 "get_db_prep_value()"。因此,Wagtail 尝试 "get_prep_value()",对于 value 中的所有 children(streamchild 实例),如下所示。 wagtail/wagtailcore/blocks/stream_block.py(第 257 行):
def get_prep_value(self, value):
if value is None:
# treat None as identical to an empty stream
return []
return [
{
'type': child.block.name,
'value': child.block.get_prep_value(child.value),
# assign a new ID on save if it didn't have one already
'id': child.id or str(uuid.uuid4()),
}
for child in value # child is a StreamChild instance
]
我不确定这个值是什么。 我的块 类 中的哪些内容需要更改才能更正此 value 变量?
Edit1- 完整错误:
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, home, sessions,
taggit, wagtailadmin, wagtailcore, wagtaildocs, wagtailembeds, wagtailforms,
wagtailimages, wagtailredirects, wagtailsearch, wagtailusers
Running migrations:
Applying home.0006_auto_20180220_1223...Traceback (most recent call last):
File "manage.py", line 12, in <module>
execute_from_command_line(sys.argv)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/core/management/__init__.py", line 363, in
execute_from_command_line
utility.execute()
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "//anaconda/envs/WagtailCMS/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 "//anaconda/envs/WagtailCMS/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 "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "//anaconda/envs/WagtailCMS/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 "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/db/migrations/operations/fields.py", line 86, in
database_forwards
field,
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/db/backends/sqlite3/schema.py", line 238, in add_field
self._remake_table(model, create_field=field)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/db/backends/sqlite3/schema.py", line 113, in _remake_table
self.effective_default(create_field)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/db/backends/base/schema.py", line 228, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/db/models/fields/__init__.py", line 766, in get_db_prep_save
prepared=False)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/django/db/models/fields/__init__.py", line 758, in get_db_prep_value
value = self.get_prep_value(value)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/wagtail/wagtailcore/fields.py", line 109, in get_prep_value
return json.dumps(self.stream_block.get_prep_value(value),
cls=DjangoJSONEncoder)
File "//anaconda/envs/WagtailCMS/lib/python3.6/site-
packages/wagtail/wagtailcore/blocks/stream_block.py", line 257, in
get_prep_value
for child in value # child is a StreamChild instance
TypeError: 'bool' object is not iterable
错误在线:
card = StreamField([('card', CardBlock())], default=True)
default
参数用于为字段指定一个default/initial值;在这种情况下,您将该值设置为 True
,这作为 StreamField 值没有意义。 (StreamField 是一个块列表,因此它正在尝试循环遍历值 True
来填充流...)
也许您的意思是 blank=False
?