إنشاء الكائنات وحفظها مباشرة
أدرج صفوفًا تفاعليًا لاختبار الأفكار بسرعة
إنشاء الكائنات وحفظها مباشرة درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Make a Row from Scratch
The shell lets you create database rows by hand. Start by building an instance with the values you want.
>>> p = Post(title="Hello", body="My first post")Nothing Saved Yet
That instance lives only in memory so far. The database knows nothing about it until you explicitly write it.
Save to Persist
Call save() to insert the row. Django runs the SQL and your object now lives in the database for real.
>>> p.save()Django Assigns an ID
After saving, your object gets a primary key in p.pk. A value means the insert succeeded and the row exists.
>>> p.pk
1The One-Step Shortcut
Prefer fewer steps? create() builds and saves in a single call, returning the finished object right away.
>>> Post.objects.create(title="Two", body="Quick")Edit Then Save Again
To update, change an attribute and call save() once more. Django writes only the new values to that row.
>>> p.title = "Edited"
>>> p.save()Read It Back
Confirm the write by fetching the row with get(). Seeing it returned proves your data really landed.
>>> Post.objects.get(pk=1)Count Your Rows
A fast sanity check is count(). It tells you how many rows the table holds after your inserts.
>>> Post.objects.count()
2Delete When Needed
Clean up test data with delete() on an instance. The row is removed from the database immediately.
>>> p.delete()It Writes to the Real DB
Remember these changes are permanent. The shell talks to your actual database, so avoid experimenting on production data.
Great for Seeding Test Data
This live flow is perfect for seeding a few rows while you build features, so pages have something to display.
Quick Check
Which call builds and saves a row in one step?
Recap
You created objects, used save() and create() to persist them, then read, counted, and deleted rows live in the shell. 💾
الأسئلة الشائعة
هل درس «إنشاء الكائنات وحفظها مباشرة» مجاني؟
نعم — نص درس «إنشاء الكائنات وحفظها مباشرة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «إنشاء الكائنات وحفظها مباشرة»؟
أدرج صفوفًا تفاعليًا لاختبار الأفكار بسرعة تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «إنشاء الكائنات وحفظها مباشرة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تشغيل python manage.py shell
- استيراد النماذج وفحصها
- إنشاء الكائنات وحفظها مباشرة
- shell_plus وأدوات أفضل