تصفية النتائج وترتيبها وتحديد عددها
استخدم filter_by وorder_by وتقسيم الصفحات.
تصفية النتائج وترتيبها وتحديد عددها درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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! 🚀
تعلم Python مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «تصفية النتائج وترتيبها وتحديد عددها» مجاني؟
نعم — نص درس «تصفية النتائج وترتيبها وتحديد عددها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «تصفية النتائج وترتيبها وتحديد عددها»؟
استخدم filter_by وorder_by وتقسيم الصفحات. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «تصفية النتائج وترتيبها وتحديد عددها»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- جلب الصفوف باستخدام query وget
- تصفية النتائج وترتيبها وتحديد عددها
- تعريف العلاقات والمفاتيح الخارجية
- عمليات الربط والتحميل الكسول مقابل المتعجل