تحديث السجلات الموجودة بأمان
غيّر الحقول وادفع التغييرات بصورة صحيحة.
تحديث السجلات الموجودة بأمان درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The U in CRUD
Updating means changing an existing row, not making a new one. The trick is to load the record first, then edit it. ✏️
Find Before You Change
Always fetch the target row by its id before touching it. get_or_404() loads it or stops with a clean 404.
user = User.query.get_or_404(user_id)Edit Like a Python Object
Once loaded, just reassign its attributes. SQLAlchemy notices the change and marks the object as dirty for you.
user.name = "New Name"Read the New Values
Updates usually carry the new data in the request body. Parse it with get_json() before applying anything.
data = request.get_json()Apply Fields Carefully
Only overwrite fields the client actually sent. Using get() with a fallback avoids wiping values to None by accident.
user.name = data.get("name", user.name)Commit the Change
No add() is needed for an existing row. A single commit() flushes your edits to the database.
db.session.commit()PUT Replaces, PATCH Tweaks
Use PUT when the client sends the whole object and PATCH when it sends only the fields it wants to change.
@app.route("/users/<int:id>", methods=["PATCH"])Validate Before Saving
Check incoming values before you commit. Reject bad data with a 400 so you never persist a broken record.
if not data.get("name"):
return jsonify(error="name required"), 400Roll Back if It Breaks
If the commit raises, call rollback() to discard the half-applied edit and leave the row exactly as it was.
except Exception:
db.session.rollback()Return the Updated Row
A good update reply sends back the fresh record with status 200, so the client sees the saved result.
return jsonify({"id": user.id, "name": user.name}), 200Load, Edit, Commit
The whole update flow is three beats: load the row, change its attributes, then commit. No new object ever appears.
Quick Check
You changed a loaded object's attribute. What must happen for the edit to stick?
Recap: Updating Records
You load with get_or_404, apply only sent fields, validate, then commit, rolling back on errors. Safe updates unlocked! 🎉
تعلم Python مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «تحديث السجلات الموجودة بأمان» مجاني؟
نعم — نص درس «تحديث السجلات الموجودة بأمان» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «تحديث السجلات الموجودة بأمان»؟
غيّر الحقول وادفع التغييرات بصورة صحيحة. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «تحديث السجلات الموجودة بأمان»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إنشاء السجلات وتثبيت الجلسات
- نقاط نهاية القراءة المفردة والمتعددة
- تحديث السجلات الموجودة بأمان
- حذف الصفوف المفقودة ومعالجتها