Shorten data to fit in the specified model field.

    If the data were too big for the field, it would cause a failure to
    insert, so we shorten it, truncating in the middle (because
    valuable information often shows up at the end.

def _truncate_to_field(model, field_name, value):
    """
    Shorten data to fit in the specified model field.

    If the data were too big for the field, it would cause a failure to
    insert, so we shorten it, truncating in the middle (because
    valuable information often shows up at the end.
    """
    field = model._meta.get_field(field_name)  # pylint: disable=protected-access
    if len(value) > field.max_length:
        midpoint = field.max_length // 2
        len_after_midpoint = field.max_length - midpoint
        first = value[:midpoint]
        sep = '...'
        last = value[len(value) - len_after_midpoint + len(sep):]
        value = sep.join([first, last])
    return value