الإنشاء
post = Post.objects.create(title="أوّل منشور", body="...")
# أو
p = Post(title="آخر")
p.save()
القراءة
Post.objects.all() # الكلّ
Post.objects.get(id=1) # واحد (يرمي خطأً إن لم يوجد)
Post.objects.filter(published=True) # تصفية
Post.objects.filter(title__contains="درس")
Post.objects.exclude(published=False)
Post.objects.order_by("-created") # ترتيب تنازلي
Post.objects.first()
Post.objects.count()
معاملات البحث (Field Lookups)
Post.objects.filter(views__gt=100) # أكبر من
Post.objects.filter(title__startswith="ك")
Post.objects.filter(created__year=2026)
التحديث
post = Post.objects.get(id=1)
post.title = "محدّث"
post.save()
Post.objects.filter(published=False).update(published=True) # دفعة
الحذف
Post.objects.get(id=1).delete()
Post.objects.filter(published=False).delete()
💡 الـ QuerySet كسول (lazy): لا يُنفَّذ الاستعلام حتى تستخدم النتيجة فعليًّا.
🎯 التالي: الـ Views بعمق.