'Prepares a value for use in a LIKE query.'

def prep_for_like_query(self, x):

    from django.utils.encoding import smart_unicode
    return smart_unicode(x).replace('\\', '\\\\').replace('%', '\\%').replace('_', '\\_')
