0
0
Djangoframework~20 mins

Custom user model with AbstractUser in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom User Model Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of accessing the custom field?
Given this custom user model extending AbstractUser, what will be the output of printing user.phone_number after creating a user with phone number '123-456-7890'?
Django
from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    phone_number = models.CharField(max_length=15, blank=True)

# Assume user is created as:
user = CustomUser.objects.create(username='testuser', phone_number='123-456-7890')
print(user.phone_number)
A123-456-7890
B'' (empty string)
Cnull
DRaises AttributeError
Attempts:
2 left
💡 Hint
Think about how model fields store data and how you access them.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a custom user model with AbstractUser?
Select the code snippet that correctly defines a custom user model by extending AbstractUser and adding a required 'birth_date' field.
A
class CustomUser(AbstractUser):
    birth_date = models.DateField(null=True, blank=True)
B
class CustomUser(AbstractUser):
    birth_date = models.DateField(null=False, blank=False)
C
class CustomUser(AbstractUser):
    birth_date = models.DateTimeField()
D
class CustomUser(AbstractUser):
    birth_date = models.CharField(max_length=10)
Attempts:
2 left
💡 Hint
A required date field should not allow null or blank values.
🔧 Debug
advanced
2:00remaining
Why does the custom user model cause a migration error?
Given this custom user model code, why does running python manage.py makemigrations raise an error about 'AUTH_USER_MODEL'?
Django
from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    nickname = models.CharField(max_length=30, blank=True)

# settings.py missing AUTH_USER_MODEL = 'app.CustomUser'
ABecause 'AUTH_USER_MODEL' is not set in settings.py to 'app.CustomUser', Django can't detect the custom user model.
BBecause AbstractUser cannot be extended with new fields.
CBecause the nickname field is missing a default value.
DBecause the CustomUser class must inherit from AbstractBaseUser, not AbstractUser.
Attempts:
2 left
💡 Hint
Check your settings.py configuration for custom user models.
state_output
advanced
2:00remaining
What is the value of user.is_staff after creation?
Consider this custom user model extending AbstractUser. After creating a user with is_staff=false, what is the value of user.is_staff?
Django
from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
    pass

user = CustomUser.objects.create(username='stafftest', is_staff=False)
print(user.is_staff)
ARaises ValueError
BTrue
CNone
DFalse
Attempts:
2 left
💡 Hint
AbstractUser has is_staff field defaulting to False.
🧠 Conceptual
expert
2:00remaining
Which statement about custom user models with AbstractUser is true?
Select the true statement about extending AbstractUser for a custom user model in Django.
ADjango automatically detects custom user models without any settings changes.
BYou must override the USERNAME_FIELD attribute when extending AbstractUser.
CExtending AbstractUser allows adding fields without redefining authentication methods.
DYou cannot use AbstractUser if you want to add any new fields to the user model.
Attempts:
2 left
💡 Hint
Think about what AbstractUser provides compared to AbstractBaseUser.