def add_attrs(widget_or_field, attrs, is_replace=True):
    """Add attributes from attrs to widget_or_field

    :param widget_or_field: object it can be a widget, field
    :param attrs: dict name, value map that contains all the attributes
     want to be added
    :param is_replace: whether replace the origin value, if not, will just
     append a space then attach the value
    """
    try:
        field = getattr(widget_or_field, 'field')  # for BoundField
    except AttributeError:
        field = widget_or_field
    try:
        widget = getattr(field, 'widget')
    except AttributeError:
        widget = field
    try:
        widget_attrs = getattr(widget, 'attrs')
    except AttributeError:
        # It's not a valid widget like object, so just do nothing
        return widget
    if is_replace:
        widget_attrs.update(attrs)
        return widget
    for name in attrs:
        if name in widget_attrs:
            widget_attrs[name] = widget_attrs[name] + ' ' + attrs[name]
        else:
            widget_attrs[name] = attrs[name]
    return widget_or_field