إنشاء السجلات وتثبيت الجلسات
أضف الكائنات واحفظها باستخدام الجلسة.
إنشاء السجلات وتثبيت الجلسات درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The C in CRUD
Creating a record means turning incoming data into a new row in your table. In Flask-SQLAlchemy that flow runs through the session. ✨
Build the Object First
Start by making a fresh model instance with the values you want. Nothing has touched the database yet, it is just a Python object.
user = User(name="Ada", email="ada@mail.com")Add It to the Session
Tell SQLAlchemy to track the new object by calling db.session.add(). This stages it, but it is still not saved.
db.session.add(user)Commit to Save
Nothing is permanent until you call db.session.commit(). That flushes your changes and writes the row to the database.
db.session.commit()The Id Appears After Commit
Once you commit, the database assigns a primary key. Read it back from user.id to confirm the row really exists.
db.session.commit()
print(user.id)A Full Create Endpoint
Inside a POST view you read the body, build the object, add it, and commit. That is the whole create path in four lines.
data = request.get_json()
user = User(name=data["name"])
db.session.add(user)
db.session.commit()Return 201 Created
A successful create should answer with status 201, the code that means a new resource was made on the server.
return jsonify(id=user.id), 201Roll Back on Failure
If a commit fails, wrap it in try and call db.session.rollback() to undo the staged change and keep your session clean.
try:
db.session.commit()
except Exception:
db.session.rollback()Add Many at Once
Need several rows? Stage them all with add_all(), then a single commit saves the whole batch together.
db.session.add_all([u1, u2, u3])
db.session.commit()One Commit, One Transaction
Every add since the last commit is part of one transaction. They all succeed together, or none of them land if it fails.
Add Stages, Commit Saves
Remember the rhythm: build the object, add to stage it, then commit to save. Skipping commit means your data never persists.
Quick Check
You called add() but the row is missing from the table. What did you forget?
Recap: Creating Records
You build an object, add it, and commit to save, returning 201 and rolling back on errors. That is a solid create endpoint! 🎉
الأسئلة الشائعة
هل درس «إنشاء السجلات وتثبيت الجلسات» مجاني؟
نعم — نص درس «إنشاء السجلات وتثبيت الجلسات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «إنشاء السجلات وتثبيت الجلسات»؟
أضف الكائنات واحفظها باستخدام الجلسة. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «إنشاء السجلات وتثبيت الجلسات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إنشاء السجلات وتثبيت الجلسات
- نقاط نهاية القراءة المفردة والمتعددة
- تحديث السجلات الموجودة بأمان
- حذف الصفوف المفقودة ومعالجتها