結果をフィルター、並べ替え、制限する
filter_by、order_by、ページネーションを使います
「結果をフィルター、並べ替え、制限する」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Narrow Results with filter_by
Use filter_by() with keyword arguments to keep only rows that match a column exactly. It reads almost like English. 🎯
admins = User.query.filter_by(role="admin").all()More Power with filter
When you need comparisons, reach for filter() and full column expressions instead of plain equality.
adults = User.query.filter(User.age >= 18).all()Combine Conditions
Pass several expressions to filter() and they join with AND, so every condition must hold for a row to match.
q = User.query.filter(User.age >= 18, User.active == True)OR Conditions
For an either-or match, wrap conditions in or_() so a row qualifies when any one of them is true.
from sqlalchemy import or_
q = User.query.filter(or_(User.age < 18, User.vip))Sort with order_by
Use order_by() to sort results by a column. By default rows come back in ascending order.
users = User.query.order_by(User.name).all()Newest First with desc
Wrap a column in desc() to flip the sort and put the largest or latest values at the top.
posts = Post.query.order_by(Post.created.desc()).all()Cap the Count with limit
Add limit() to fetch only the first N rows. It keeps responses small and queries fast.
top = Post.query.order_by(Post.score.desc()).limit(5).all()Skip Rows with offset
Use offset() to skip a number of rows before returning. Combined with limit, it gives you simple paging.
page2 = Post.query.limit(10).offset(10).all()Real Pagination with paginate
Flask-SQLAlchemy bundles paging into paginate(), which returns items plus handy page metadata in one call.
p = Post.query.paginate(page=2, per_page=10)Chain It All Together
Filter, order, and limit chain fluently, and nothing runs until you finish with all or first.
q = Post.query.filter_by(public=True).order_by(Post.id.desc()).limit(3)Order Matters for Limits
Always pair limit() with order_by. Without a sort, which rows you get back is unpredictable.
Quick Check
You want the three highest-scoring posts. Which clause makes the score the top result?
Recap: Shaping Results
You can now filter, sort with order_by and desc, and trim output with limit, offset, and paginate. Powerful reads unlocked! 🚀
よくある質問
「結果をフィルター、並べ替え、制限する」レッスンは無料ですか?
はい。「結果をフィルター、並べ替え、制限する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「結果をフィルター、並べ替え、制限する」で何を学びますか?
filter_by、order_by、ページネーションを使います ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「結果をフィルター、並べ替え、制限する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- queryとgetで行を取得する
- 結果をフィルター、並べ替え、制限する
- リレーションシップと外部キーを定義する
- JOINと遅延読み込み・即時読み込み