if page == 1 and len(items) < per_page:
    total = len(items)
else:
    total = query.order_by(None).count()
return Pagination(query, page, per_page, total, items)
How to paginate in Flask-SQLAlchemy for db.session joined queries?