'Returns field\'s value prepared for database lookup.'

def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False):

    if (not prepared):
        value = self.get_prep_lookup(lookup_type, value)
        prepared = True
    if hasattr(value, u'get_compiler'):
        value = value.get_compiler(connection=connection)
    if (hasattr(value, u'as_sql') or hasattr(value, u'_as_sql')):
        if hasattr(value, u'relabeled_clone'):
            return value
        if hasattr(value, u'as_sql'):
            (sql, params) = value.as_sql()
        else:
            (sql, params) = value._as_sql(connection=connection)
        return QueryWrapper((u'(%s)' % sql), params)
    if (lookup_type in (u'month', u'day', u'week_day', u'hour', u'minute', u'second', u'search', u'regex', u'iregex')):
        return [value]
    elif (lookup_type in (u'exact', u'gt', u'gte', u'lt', u'lte')):
        return [self.get_db_prep_value(value, connection=connection, prepared=prepared)]
    elif (lookup_type in (u'range', u'in')):
        return [self.get_db_prep_value(v, connection=connection, prepared=prepared) for v in value]
    elif (lookup_type in (u'contains', u'icontains')):
        return [(u'%%%s%%' % connection.ops.prep_for_like_query(value))]
    elif (lookup_type == u'iexact'):
        return [connection.ops.prep_for_iexact_query(value)]
    elif (lookup_type in (u'startswith', u'istartswith')):
        return [(u'%s%%' % connection.ops.prep_for_like_query(value))]
    elif (lookup_type in (u'endswith', u'iendswith')):
        return [(u'%%%s' % connection.ops.prep_for_like_query(value))]
    elif (lookup_type == u'isnull'):
        return []
    elif (lookup_type == u'year'):
        if isinstance(self, DateTimeField):
            return connection.ops.year_lookup_bounds_for_datetime_field(value)
        elif isinstance(self, DateField):
            return connection.ops.year_lookup_bounds_for_date_field(value)
        else:
            return [value]
