0Pricing
Django Academy · درس

خيارات الحقل: null وblank وdefault

تحكّم في القيم التي يقبلها العمود

خيارات الحقل: null وblank وdefault درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Tune Each Field

Beyond the type, every field accepts options that control what values it will accept and how it behaves.

null Is About the Database

The null option decides whether the database column may store NULL. With null=True, the column can be empty in the database.

summary = models.TextField(null=True)

blank Is About Forms

The blank option controls validation, not the database. With blank=True, forms allow the field to be left empty.

summary = models.TextField(blank=True)

null and blank Are Different

Remember the split: null affects the database layer, while blank affects form validation. They solve two separate problems.

Avoid null on Text Fields

For string fields, skip null and just use blank=True. Django prefers an empty string over NULL to keep two empty states from existing.

note = models.CharField(max_length=100, blank=True)

default Supplies a Value

The default option sets the value used when none is given, so new rows start with sensible data.

views = models.IntegerField(default=0)

Callable Defaults

A default can be a callable like timezone.now. Pass the function itself, with no parentheses, so it runs for each new row.

created = models.DateTimeField(default=timezone.now)

unique Prevents Duplicates

Add unique=True and the database refuses two rows with the same value, perfect for usernames or slugs. 🚫

username = models.CharField(max_length=50, unique=True)

choices Limits the Options

The choices option restricts a field to a fixed set of values and renders a dropdown in forms and the admin.

STATUS = [("d", "Draft"), ("p", "Published")]
status = models.CharField(max_length=1, choices=STATUS)

Options Stack Together

You can combine options freely on one field, like blank=True with default. Stack them to express exactly what you need.

tagline = models.CharField(max_length=120, blank=True, default="")

Changes Need a Migration

Editing an option changes the schema. After tweaking null, default, or unique, run a migration so the database matches.

Quick Check

What is the real difference between null and blank?

Recap: Field Options

You now know that null is database-level, blank is form-level, default seeds values, and unique blocks duplicates. Tune fields with confidence! ✨

الأسئلة الشائعة

هل درس «خيارات الحقل: null وblank وdefault» مجاني؟

نعم — نص درس «خيارات الحقل: null وblank وdefault» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.

ماذا ستتعلم في «خيارات الحقل: null وblank وdefault»؟

تحكّم في القيم التي يقبلها العمود تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟

لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «خيارات الحقل: null وblank وdefault»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟

نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تعريف فئة Model
  2. اختيار أنواع الحقول
  3. خيارات الحقل: null وblank وdefault
  4. ‏str وفئة Meta
← العودة إلى Django Academy