0
0
Djangoframework~20 mins

Defining models with fields in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Model Fields Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django model field default value?
Consider this Django model field definition:
age = models.IntegerField(default=20)

What will be the value of age if a new model instance is created without specifying age?
AThe age field will raise a validation error for missing value.
BThe age field will be set to None.
CThe age field will be set to 20 by default.
DThe age field will be set to 0 by default.
Attempts:
2 left
💡 Hint
Think about what the default parameter does in Django model fields.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a CharField with max length 50 and allows blank values?
You want to define a Django model field that stores text up to 50 characters and can be left blank. Which option is correct?
Aname = models.CharField(max_length=50, blank=True)
Bname = models.CharField(max_length=50, null=True)
Cname = models.CharField(blank=True)
Dname = models.CharField(max_length=50, blank=False)
Attempts:
2 left
💡 Hint
Remember that max_length is required for CharField and blank=True allows empty input.
🔧 Debug
advanced
2:00remaining
What error does this model field definition cause?
Examine this Django model field:
price = models.DecimalField(max_digits=5, decimal_places=2, max_length=10)

What error will this cause when running migrations?
AValueError: max_digits must be greater than decimal_places
BTypeError: DecimalField got an unexpected keyword argument 'max_length'
CNo error, field is valid
DSyntaxError: invalid syntax in field definition
Attempts:
2 left
💡 Hint
Check the valid parameters for DecimalField.
state_output
advanced
2:00remaining
What is the value of 'is_active' after saving this model instance?
Given this model field:
is_active = models.BooleanField(default=True)

And this code:
obj = MyModel()
obj.is_active = False
obj.save()

What will be the value of is_active in the database?
AFalse, because the field was explicitly set before saving.
BTrue, because default=True overrides assigned values.
CRaises IntegrityError due to missing value.
DNone, because BooleanField cannot be set to False.
Attempts:
2 left
💡 Hint
Default is only used if no value is assigned.
🧠 Conceptual
expert
3:00remaining
Which field option combination ensures a unique, non-null email field that can be blank in forms?
You want to define an email field in a Django model that:
- Must be unique in the database
- Cannot be null in the database
- Can be left blank in forms (optional input)
Which option correctly achieves this?
Aemail = models.EmailField(unique=True, null=True, blank=True)
Bemail = models.EmailField(unique=True, null=True, blank=False)
Cemail = models.EmailField(unique=False, null=False, blank=True)
Demail = models.EmailField(unique=True, null=False, blank=True)
Attempts:
2 left
💡 Hint
null controls database nullability; blank controls form validation.