0
0
Djangoframework~20 mins

Field options (max_length, null, blank, default) in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Field Options Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a CharField has blank=True but null=False?

Consider this Django model field:

name = models.CharField(max_length=50, blank=True, null=False)

What is the behavior when a form submits an empty string for this field?

AThe form will raise a validation error because blank=True and null=False conflict.
BThe database stores NULL because blank=True allows empty input and null=True is required to store NULL.
CThe database stores an empty string ('') because blank=True allows empty input, but null=False prevents NULL values.
DThe database stores the string 'null' as text because null=False is set.
Attempts:
2 left
💡 Hint

Think about what blank and null control separately: form validation vs database storage.

📝 Syntax
intermediate
2:00remaining
Which field declaration correctly sets a default value for a CharField?

Choose the correct Django model field declaration that sets a default value of 'guest' for a username field.

Ausername = models.CharField(max_length=30, default='')
Busername = models.CharField(max_length=30, default='guest')
Cusername = models.CharField(max_length=30, default=None)
Dusername = models.CharField(max_length=30, default=guest)
Attempts:
2 left
💡 Hint

Remember that default values for CharField must be strings.

state_output
advanced
2:00remaining
What is the value stored in the database for a TextField with null=True and blank=True when left empty?

Given this model field:

description = models.TextField(null=True, blank=True)

If a form submits no input for description, what value is stored in the database?

AA validation error occurs because blank=True and null=True conflict.
BAn empty string ('') is stored because blank=True allows empty input.
CThe string 'null' is stored as text.
DNULL is stored in the database because null=True allows NULL values.
Attempts:
2 left
💡 Hint

Consider how null and blank affect database storage and form validation separately.

🔧 Debug
advanced
2:00remaining
Why does this model field cause a validation error on empty input?

Examine this Django model field:

email = models.EmailField(max_length=100, null=True, blank=False)

When submitting a form with an empty email, a validation error occurs. Why?

ABecause blank=False requires the form to have a non-empty value, so empty input is invalid.
BBecause null=True conflicts with blank=False causing a syntax error.
CBecause max_length=100 is too short for empty input.
DBecause EmailField does not support null=True.
Attempts:
2 left
💡 Hint

Think about what blank controls in form validation.

🧠 Conceptual
expert
3:00remaining
How do max_length and default interact in a CharField?

Consider this field:

title = models.CharField(max_length=5, default='HelloWorld')

What happens when this model is saved without specifying title?

ADjango raises a validation error before saving due to default length.
BThe default value is truncated to 'Hello' and saved without error.
CThe model saves successfully with the full default string 'HelloWorld'.
DA database error occurs because the default string exceeds max_length.
Attempts:
2 left
💡 Hint

Think about Django's validation process before saving a model.