# Buggy code
def get_searchable_content(self, value):
    # Return the display value as the searchable value
    text_value = force_text(value)
    for k, v in self.field.choices:
        if isinstance(v, (list, tuple)):
            # This is an optgroup, so look inside the group for options
            for k2, v2 in v:
                if value == k2 or text_value == force_text(k2):
                    return [k, v2]
        else:
            if value == k or text_value == force_text(k):
                return [v]
    return []  # Value was not found in the list of choices


# Traceback (TypeError)
→ python manage.py update_index
Updating backend: default
default: Rebuilding index wagtail_serialization
default: home.HomePage             Traceback (most recent call last):
File "/Users/moritz/python/buchbasel/lib/python3.4/site-packages/elasticsearch/serializer.py", line 45, in dumps
return json.dumps(data, default=self.default)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/__init__.py", line 237, in dumps
**kw).encode(obj)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/encoder.py", line 192, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/encoder.py", line 250, in iterencode
return _iterencode(o, 0)
File "/Users/moritz/python/buchbasel/lib/python3.4/site-packages/elasticsearch/serializer.py", line 31, in default
raise TypeError("Unable to serialize %r (type: %s)" % (data, type(data)))
TypeError: Unable to serialize <django.utils.functional.lazy.<locals>.__proxy__ object at 0x111889fd0> (type: <class 'django.utils.functional.lazy.<locals>.__proxy__'>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "manage.py", line 12, in <module>
execute_from_command_line(sys.argv)
File "/Users/moritz/python/buchbasel/lib/python3.4/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "/Users/moritz/python/buchbasel/lib/python3.4/site-packages/django/core/management/__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/moritz/python/buchbasel/lib/python3.4/site-packages/django/core/management/base.py", line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/moritz/python/buchbasel/lib/python3.4/site-packages/django/core/management/base.py", line 445, in execute
output = self.handle(*args, **options)
File "/Users/moritz/python/buchbasel/lib/python3.4/site-packages/wagtail/wagtailsearch/management/commands/update_index.py", line 120, in handle
self.update_backend(backend_name, schema_only=options['schema_only'])
File "/Users/moritz/python/buchbasel/lib/python3.4/site-packages/wagtail/wagtailsearch/management/commands/update_index.py", line 87, in update_backend
index.add_items(model, chunk)
File "/Users/moritz/python/buchbasel/lib/python3.4/site-packages/wagtail/wagtailsearch/backends/elasticsearch.py", line 529, in add_items
bulk(self.es, actions)
File "/Users/moritz/python/buchbasel/lib/python3.4/site-packages/elasticsearch/helpers/__init__.py", line 188, in bulk
for ok, item in streaming_bulk(client, actions, **kwargs):
File "/Users/moritz/python/buchbasel/lib/python3.4/site-packages/elasticsearch/helpers/__init__.py", line 159, in streaming_bulk
for bulk_actions in _chunk_actions(actions, chunk_size, max_chunk_bytes, client.transport.serializer):
File "/Users/moritz/python/buchbasel/lib/python3.4/site-packages/elasticsearch/helpers/__init__.py", line 58, in _chunk_actions
data = serializer.dumps(data)
File "/Users/moritz/python/buchbasel/lib/python3.4/site-packages/elasticsearch/serializer.py", line 47, in dumps
raise SerializationError(data, e)
elasticsearch.exceptions.SerializationError: ({'title': 'Homepage', 'path_filter': '00010001', 'live_filter': True, 'body': [<django.utils.functional.lazy.<locals>.__proxy__ object at 0x111889fd0>], 'content_type_id_filter': 2, 'content_type': 'wagtailcore_page_home_homepage', 'id_filter': 3, 'show_in_menus_filter': False, '_partials': ['Homepage'], 'depth_filter': 2, 'first_published_at_filter': datetime.datetime(2016, 8, 19, 6, 19, 24, 47945, tzinfo=<UTC>), 'owner_id_filter': None, 'locked_filter': False, 'pk': '3', 'latest_revision_created_at_filter': datetime.datetime(2016, 8, 19, 6, 19, 23, 999422, tzinfo=<UTC>)}, TypeError("Unable to serialize <django.utils.functional.lazy.<locals>.__proxy__ object at 0x111889fd0> (type: <class 'django.utils.functional.lazy.<locals>.__proxy__'>)",))

# Fixed code
def get_searchable_content(self, value):
    # Return the display value as the searchable value
    text_value = force_text(value)
    for k, v in self.field.choices:
        if isinstance(v, (list, tuple)):
            # This is an optgroup, so look inside the group for options
            for k2, v2 in v:
                if value == k2 or text_value == force_text(k2):
                    return [force_text(k), force_text(v2)]
        else:
            if value == k or text_value == force_text(k):
                return [force_text(v)]
    return []  # Value was not found in the list of choices