def search(cls, term, fields=()):
        if not any((cls._meta.search_fields, fields)):
            raise AttributeError(
                "A list of searchable fields must be provided in the class's "
                "search_fields or provided to this function in the `fields` "
                "kwarg."
            )
        if not fields:
            fields = cls._meta.search_fields
        query = cls.select()
        like_term = ''.join((term, '%'))
        full_like_term = ''.join(('%', term, '%'))
        order_by = []
        clauses = []
        for field_name in fields:
            field = getattr(cls, field_name)
            clauses.append(
                (field == term) |
                (field ** like_term) |
                (field ** full_like_term)
            )
            order_by.append(case(None, (
                (field == term, 0),
                (field ** like_term, 1),
                (field ** full_like_term, 2),
            ), default=3).asc())
        query = query.where(reduce(operator.or_, clauses))
        query = query.order_by(*order_by)
        return query