Bird
Raised Fist0
Djangoframework~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main reason to create a custom user model by extending AbstractUser in Django?
easy
A. To add extra fields or change user behavior while keeping Django's default features
B. To remove all default user features and start from scratch
C. To automatically create admin users without configuration
D. To avoid using migrations in the project

Solution

  1. Step 1: Understand AbstractUser purpose

    AbstractUser provides Django's default user fields and behavior as a base class.
  2. Step 2: Reason for extending AbstractUser

    Extending it allows adding custom fields or changing behavior without losing built-in features.
  3. Final Answer:

    To add extra fields or change user behavior while keeping Django's default features -> Option A
  4. Quick Check:

    Custom user model = Extend AbstractUser for extra fields [OK]
Hint: AbstractUser keeps defaults; extend it to add fields [OK]
Common Mistakes:
  • Thinking AbstractUser removes default features
  • Believing custom user models skip migrations
  • Assuming admin users are auto-created
2. Which of the following is the correct way to declare a custom user model by extending AbstractUser in Django?
easy
A. class CustomUser(AbstractBaseUser):\n pass
B. class CustomUser(User):\n pass
C. class CustomUser(models.Model):\n pass
D. class CustomUser(AbstractUser):\n pass

Solution

  1. Step 1: Identify correct base class

    The question asks for extending AbstractUser, so the class must inherit from it.
  2. Step 2: Check syntax correctness

    class CustomUser(AbstractUser):\n pass correctly defines class CustomUser(AbstractUser): pass which is valid syntax.
  3. Final Answer:

    class CustomUser(AbstractUser):\n pass -> Option D
  4. Quick Check:

    Extend AbstractUser with class CustomUser(AbstractUser) [OK]
Hint: Use AbstractUser as base class for custom user model [OK]
Common Mistakes:
  • Using User instead of AbstractUser as base
  • Inheriting directly from models.Model without user features
  • Confusing AbstractBaseUser with AbstractUser
3. Given this custom user model:
from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    age = models.PositiveIntegerField(null=True, blank=True)

# settings.py
AUTH_USER_MODEL = 'myapp.CustomUser'

What will happen if you try to create a user without specifying age?
medium
A. User creation fails due to missing age field
B. User is created successfully with age set to None
C. User is created but age defaults to 0
D. Error because age is required

Solution

  1. Step 1: Analyze age field definition

    Age is defined as PositiveIntegerField with null=True and blank=True, so it is optional.
  2. Step 2: Understand user creation behavior

    Since age is optional, creating a user without it sets age to None (null in database).
  3. Final Answer:

    User is created successfully with age set to None -> Option B
  4. Quick Check:

    Optional field with null=True allows missing value [OK]
Hint: null=True means field can be empty on creation [OK]
Common Mistakes:
  • Assuming blank=True means field is required
  • Thinking missing fields default to 0 automatically
  • Confusing null=True with default values
4. You created a custom user model extending AbstractUser and set AUTH_USER_MODEL in settings. After running migrations, you get an error about conflicting user models. What is the most likely cause?
medium
A. You set AUTH_USER_MODEL after initial migrations were created
B. You forgot to import AbstractUser in your model
C. You did not define a primary key in your custom user model
D. You used AbstractBaseUser instead of AbstractUser

Solution

  1. Step 1: Understand migration timing

    If AUTH_USER_MODEL is set after initial migrations, Django creates default user tables causing conflicts.
  2. Step 2: Identify cause of conflict error

    The conflict arises because two user models exist: default and custom, due to late setting of AUTH_USER_MODEL.
  3. Final Answer:

    You set AUTH_USER_MODEL after initial migrations were created -> Option A
  4. Quick Check:

    Set AUTH_USER_MODEL before first migration [OK]
Hint: Set AUTH_USER_MODEL before first migration to avoid conflicts [OK]
Common Mistakes:
  • Ignoring migration order importance
  • Assuming import errors cause this conflict
  • Confusing AbstractUser with AbstractBaseUser issues
5. You want to add a bio text field to your custom user model extending AbstractUser. You also want to display this bio in Django admin user list view. Which steps should you follow?
hard
A. Add bio field to model, override save() to print bio
B. Add bio field to model, no admin changes needed
C. Add bio field to model, register custom user admin with list_display including 'bio'
D. Add bio field to model, create a new admin site

Solution

  1. Step 1: Add bio field to custom user model

    Define bio = models.TextField(blank=True, null=True) in your model to store user bios.
  2. Step 2: Customize admin to show bio

    Register your custom user model admin and set list_display = ('username', 'email', 'bio') to show bio in list view.
  3. Final Answer:

    Add bio field to model, register custom user admin with list_display including 'bio' -> Option C
  4. Quick Check:

    Model field + admin list_display shows field [OK]
Hint: Add field + update admin list_display to show it [OK]
Common Mistakes:
  • Forgetting to update admin list_display
  • Overriding save() unnecessarily
  • Creating new admin site instead of customizing existing