Flask Academy · درس

نقاط نهاية القراءة المفردة والمتعددة

أعِد عنصرًا واحدًا أو مجموعة.

الدرس 2 من 413 خطوة

نقاط نهاية القراءة المفردة والمتعددة درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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"), 404

Skip 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! 🎉

البدء مجانًا

تعلم Python مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
30
الدروس
120

الأسئلة الشائعة

هل درس «نقاط نهاية القراءة المفردة والمتعددة» مجاني؟

نعم — نص درس «نقاط نهاية القراءة المفردة والمتعددة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.

ماذا ستتعلم في «نقاط نهاية القراءة المفردة والمتعددة»؟

أعِد عنصرًا واحدًا أو مجموعة. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟

لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «نقاط نهاية القراءة المفردة والمتعددة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟

نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. إنشاء السجلات وتثبيت الجلسات
  2. نقاط نهاية القراءة المفردة والمتعددة
  3. تحديث السجلات الموجودة بأمان
  4. حذف الصفوف المفقودة ومعالجتها
← العودة إلى Flask Academy