جلب الصفوف باستخدام query وget
استرجع جميع الصفوف أو صفًا واحدًا أو صفًا حسب المفتاح الأساسي.
جلب الصفوف باستخدام query وget درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Reading Starts With query
Every read from your database begins with the model's query attribute. It is your gateway to fetching rows as Python objects. 🔎
rows = User.queryGrab Everything with all
Call all() on a query to load every matching row into a plain Python list you can loop over.
users = User.query.all()
for u in users:
print(u.name)Just One With first
Use first() when you want a single row. It returns the first match, or None if the table has nothing to give.
user = User.query.first()Fetch by Primary Key
When you know the id, get() is the fastest path. Hand it the primary key and it returns that exact row.
user = User.query.get(7)get Returns None When Missing
If no row has that id, get() quietly returns None instead of raising. Always check before you use the result.
user = User.query.get(999)
if user is None:
print("not found")404 the Easy Way
In a view, get_or_404() fetches by id and auto-aborts with a 404 page when the row is absent. No manual check needed.
user = User.query.get_or_404(7)first_or_404 for Filtered Reads
Pair first_or_404() with a filter to grab one match or raise a clean 404 when nothing fits.
u = User.query.filter_by(email=e).first_or_404()Count Without Loading Rows
Need a tally, not the data? count() asks the database for the number directly, so you never pull full rows into memory.
total = User.query.count()Queries Are Lazy
Building a query does not touch the database yet. The SQL only runs when you call a method like all, first, or get.
q = User.query
results = q.all()Each Row Is an Object
Results come back as real model instances, so you read columns as ordinary attributes like user.name and user.email.
user = User.query.first()
print(user.email)Pick the Right Fetch
Use get for a known id, first for one of many, and all for the whole set. Choosing well keeps reads clear and fast.
Quick Check
You know a record's primary key. Which call fetches it most directly?
Recap: Fetching Rows
You now read data with query: all for lists, first for one, get by id, and the or_404 helpers for clean views. Nice work! 🎉
الأسئلة الشائعة
هل درس «جلب الصفوف باستخدام query وget» مجاني؟
نعم — نص درس «جلب الصفوف باستخدام query وget» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «جلب الصفوف باستخدام query وget»؟
استرجع جميع الصفوف أو صفًا واحدًا أو صفًا حسب المفتاح الأساسي. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «جلب الصفوف باستخدام query وget»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- جلب الصفوف باستخدام query وget
- تصفية النتائج وترتيبها وتحديد عددها
- تعريف العلاقات والمفاتيح الخارجية
- عمليات الربط والتحميل الكسول مقابل المتعجل