Read One and Read Many Endpoints
Return a single item or a collection.
Read One and Read Many Endpoints is a free Flask Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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! 🎉
Frequently asked questions
Is the “Read One and Read Many Endpoints” lesson free?
Yes — the full text of “Read One and Read Many Endpoints” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Read One and Read Many Endpoints”?
Return a single item or a collection. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Read One and Read Many Endpoints” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Create Records and Commit Sessions
- Read One and Read Many Endpoints
- Update Existing Records Safely
- Delete and Handle Missing Rows