0
0
Djangoframework~5 mins

Field options (max_length, null, blank, default) in Django - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the max_length option do in a Django model field?
It sets the maximum number of characters allowed in a text-based field like CharField. This helps limit the size of the data stored.
Click to reveal answer
beginner
What is the difference between null=True and blank=True in Django model 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 and validations.
Click to reveal answer
beginner
How does the default option work in Django model fields?
It sets a default value for the field if no value is provided when creating a model instance. This helps avoid errors and ensures a value is always present.
Click to reveal answer
intermediate
Can you use blank=True without null=True? What does it mean?
Yes. It means the field can be empty in forms, but the database still requires a value (not NULL). For text fields, empty string ('') is stored instead of NULL.
Click to reveal answer
beginner
Why is it important to set max_length for CharField in Django?
Because the database needs to know the maximum size to allocate space. It also helps validate user input early and avoid storing too long strings.
Click to reveal answer
What does null=True allow in a Django model field?
ALimits the maximum length
BThe field can be left empty in forms
CSets a default value
DThe database can store NULL for this field
Which option lets a form accept an empty value for a field?
Ablank=True
Bnull=True
Cmax_length
Ddefault
What happens if you set default='N/A' on a field?
AThe field will use 'N/A' if no value is given
BThe field must always be filled manually
CThe field cannot be empty in forms
DThe database will reject NULL values
If a CharField has blank=True but not null=True, what is stored in the database when left empty?
ANULL
BZero
CEmpty string ('')
DDefault value
Why should you always set max_length on CharField?
ATo allow NULL values
BTo limit the size of data stored
CTo set a default value
DTo make the field optional
Explain the roles of max_length, null, blank, and default options in Django model fields.
Think about how each option affects data storage and user input.
You got /4 concepts.
    Describe a situation where you would use null=True but not blank=True in a Django model field.
    Consider when data can be missing in the database but must be provided via forms.
    You got /3 concepts.