単一取得と複数取得のエンドポイント
単一の項目またはコレクションを返します
「単一取得と複数取得のエンドポイント」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Two Shapes of Read
A REST resource needs two read endpoints: one that returns a single item by id, and one that returns the whole collection. 📚
List Route for Many
The collection lives at a plain path like /users. A GET there should answer with every matching record.
@app.route("/users")
def list_users():
...Load Them All
Fetch the full set with query.all(), which gives you a Python list of model instances to serialize.
users = User.query.all()Serialize the Collection
jsonify cannot dump model objects directly, so map each row to a dict first, then return the list.
return jsonify([{"id": u.id, "name": u.name} for u in users])Detail Route for One
A single item lives at /users/<id>. The id in the path tells you exactly which row to load.
@app.route("/users/<int:user_id>")
def get_user(user_id):
...Fetch That One Row
Use get() with the path id to pull the matching record straight from the database.
user = User.query.get(user_id)Return the Single Item
Wrap the found row in a dict and jsonify it. Your client gets one clean JSON object back.
return jsonify({"id": user.id, "name": user.name})Handle the Missing Case
If get returns None, the id was wrong, so reply with a 404 instead of crashing on a None value.
if user is None:
return jsonify(error="not found"), 404Skip the Check with get_or_404
Flask-SQLAlchemy's get_or_404() fetches by id or aborts with 404 automatically, trimming the boilerplate from detail views.
user = User.query.get_or_404(user_id)Paginate Long Lists
Returning thousands of rows is slow. Use paginate() to slice the collection into pages your client can request one at a time.
page = User.query.paginate(page=1, per_page=20)List Returns an Array
Keep shapes predictable: the collection route returns a JSON array, while the detail route returns a single object.
Quick Check
A request hits /users/42 but no such row exists. What should the endpoint do?
Recap: Reading Records
You built a list route returning an array and a detail route returning one item, with 404 for missing rows. Reads done right! 🎉
よくある質問
「単一取得と複数取得のエンドポイント」レッスンは無料ですか?
はい。「単一取得と複数取得のエンドポイント」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「単一取得と複数取得のエンドポイント」で何を学びますか?
単一の項目またはコレクションを返します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「単一取得と複数取得のエンドポイント」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- レコードを作成してセッションをコミットする
- 単一取得と複数取得のエンドポイント
- 既存レコードを安全に更新する
- 削除と存在しない行の処理