Reputation: 43
Build: Wagtail CMS(1.13.1) with Django(1.11) in python3.6.
I am trying to create a pretty basic Streamfield block(CardBlock), but keep getting a type error. It is very similar to the examples in documentation, but I can not get it to work...
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 is trying to "get_db_prep_value()". So, Wagtail tries to "get_prep_value()", for all children(streamchild instances) in value, as shown below. wagtail/wagtailcore/blocks/stream_block.py (line 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
]
I am uncertain as to what this value is. What in my block classes needs to be changed to correct this value variable?
Edit1- Full Error:
$ 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
Upvotes: 0
Views: 1938
Reputation: 25227
The error is on the line:
card = StreamField([('card', CardBlock())], default=True)
The default
parameter is used to specify a default/initial value for the field; in this case, you're setting that value to True
, which doesn't make sense as a StreamField value. (A StreamField is a list of blocks, so it's trying and failing to loop over the value True
to populate the stream...)
Perhaps you meant blank=False
instead?
Upvotes: 1