Field options help control how data is stored and validated in your database. They make sure your data fits rules you set, like length limits or default values.
Field options (max_length, null, blank, default) in Django
field_name = models.CharField(max_length=100, null=True, blank=True, default='example')
max_length sets the maximum number of characters for text fields.
null=True means the database can store a NULL value for this field.
blank=True means the field can be left empty in forms.
default sets a value automatically if none is given.
name = models.CharField(max_length=50)description = models.TextField(null=True, blank=True)
status = models.CharField(max_length=20, default='pending')
This model defines a product with a name, optional description, and a status that defaults to 'available'. When creating a product without description or status, description is None and status is 'available'.
from django.db import models class Product(models.Model): name = models.CharField(max_length=100) description = models.TextField(null=True, blank=True) status = models.CharField(max_length=20, default='available') # Example usage: product = Product(name='Coffee Mug') print(product.name) # Coffee Mug print(product.description) # None print(product.status) # available
Use null=True only for non-string fields or when you want to store NULL in the database.
blank=True affects form validation, allowing empty input.
Always set max_length for CharField to avoid errors.
max_length limits text size.
null controls database NULL values.
blank controls form input allowance.
default sets a fallback value.
Practice
CharField?Solution
Step 1: Understand the purpose of
Themax_lengthmax_lengthoption sets the maximum number of characters allowed in text-based fields likeCharField.Step 2: Compare with other options
nullcontrols database NULL values,blankcontrols form validation, anddefaultsets a fallback value, so they do not limit text length.Final Answer:
max_length -> Option CQuick Check:
max_length = max characters allowed [OK]
- Confusing null with max_length
- Thinking blank limits text length
- Using default to restrict input size
Solution
Step 1: Understand
blankandnulldifferencesblank=Trueallows empty input in forms;null=Falsemeans database does not store NULL but uses empty string or default.Step 2: Check other options
blank=True, null=Trueallows NULL in database;blank=Falsedisallows empty form input.Final Answer:
blank=True, null=False -> Option BQuick Check:
blank=True allows empty form, null=False disallows NULL in DB [OK]
- Mixing up blank and null meanings
- Assuming null=True always needed for empty forms
- Setting blank=False but expecting empty input
name = models.CharField(max_length=10, null=True, blank=True, default='Unknown')
What will be the value of
name if a form submits an empty string for this field?Solution
Step 1: Analyze blank and null behavior
blank=Trueallows empty string input;null=Trueallows NULL in DB but empty string is valid input.Step 2: Understand default usage
Default is used only if no value is provided; submitting empty string counts as a value, so default is ignored.Final Answer:
'' (empty string) -> Option AQuick Check:
Empty string input saved as '' not default or None [OK]
- Thinking default applies on empty string input
- Confusing null=True with empty string storage
- Expecting validation error for blank=True
age = models.IntegerField(null=False, blank=True)
What is the main issue with this declaration?
Solution
Step 1: Understand null=False and blank=True effects
null=Falsemeans database does not accept NULL;blank=Trueallows empty form input.Step 2: Identify validation conflict
Allowing empty form input but disallowing NULL in DB causes validation errors because empty input cannot be saved as NULL.Final Answer:
It disallows NULL in the database but allows empty form input, causing validation errors -> Option DQuick Check:
blank=True with null=False causes validation mismatch [OK]
- Assuming blank=True always works with null=False
- Thinking default is set automatically
- Confusing max_length with IntegerField
CharField that:- Accepts empty input in forms
- Stores NULL in the database when empty
- Has a default value of 'N/A'
Which field declaration achieves this correctly?
Solution
Step 1: Match requirements to options
Accept empty input in forms requiresblank=True. Store NULL in DB when empty requiresnull=True. Default value set bydefault='N/A'.Step 2: Verify each option
models.CharField(max_length=20, blank=True, null=True, default='N/A')has all three correctly set. Declarations withblank=Falsedisallow empty form input.models.CharField(max_length=20, blank=True, null=False, default='N/A')disallows NULL in DB.Final Answer:
models.CharField(max_length=20, blank=True, null=True, default='N/A') -> Option AQuick Check:
blank=True + null=True + default='N/A' meets all needs [OK]
- Forgetting null=True to store NULL in DB
- Setting blank=False disallows empty form input
- Assuming default overrides empty input
Ready to prove it?
Pass the test and this pattern moves unknown
