0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-25 13:10:14 +01:00

Discard invalid StreamField data rather than throwing ValueError

This commit is contained in:
Matt Westcott 2015-03-05 10:36:39 +00:00
parent 6f37c78c95
commit b2372c8787

View File

@ -70,7 +70,16 @@ class StreamField(with_metaclass(models.SubfieldBase, models.Field)):
elif isinstance(value, StreamValue):
return value
else: # assume string
return self.stream_block.to_python(json.loads(value))
try:
return self.stream_block.to_python(json.loads(value))
except ValueError:
# value is not valid JSON; most likely, this field was previously a
# rich text field before being migrated to StreamField, and the data
# was left intact in the migration. Return an empty stream instead.
# TODO: keep this raw text data around as a property of the StreamValue
# so that it can be retrieved in data migrations
return StreamValue(self.stream_block, [])
def get_prep_value(self, value):
return json.dumps(self.stream_block.get_prep_value(value), cls=DjangoJSONEncoder)