Bird
Raised Fist0
Djangoframework~20 mins

Database configuration in Django - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Django Database Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
1:30remaining
What is the default database engine in Django's settings?
In a new Django project, what database engine is used by default in the DATABASES setting?
Django
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
ASQLite
BMySQL
CPostgreSQL
DOracle
Attempts:
2 left
💡 Hint
Think about the simplest database that requires no extra setup.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Django database configuration
Which option contains a syntax error in the DATABASES setting for PostgreSQL?
Django
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydb',
        'USER': 'user',
        'PASSWORD': 'pass',
        'HOST': 'localhost',
        'PORT': '5432'
    }
}
ADATABASES = {'default': {'ENGINE': 'django.db.backends.postgresql', 'NAME': 'mydb', 'USER': 'user', 'PASSWORD': 'pass', 'HOST': 'localhost', 'PORT': '5432'}}
BDATABASES = {'default': {'ENGINE': 'django.db.backends.postgresql', 'NAME': 'mydb', 'USER': 'user', 'PASSWORD': 'pass', 'HOST': 'localhost' 'PORT': '5432'}}
C}}'2345' :'TROP' ,'tsohlacol' :'TSOH' ,'ssap' :'DROWSSAP' ,'resu' :'RESU' ,'bdym' :'EMAN' ,'lqsergtsop.sdnekcab.bd.ognajd' :'ENIGNE'{ :'tluafed'{ = SESABATAD
DDATABASES = {'default': {'ENGINE': 'django.db.backends.postgresql', 'NAME': 'mydb', 'USER': 'user', 'PASSWORD': 'pass', 'HOST': 'localhost', 'PORT': 5432}}
Attempts:
2 left
💡 Hint
Look for missing punctuation between keys.
❓ state_output
advanced
1:30remaining
What will be the database NAME after this settings update?
Given this initial DATABASES setting, what is the value of DATABASES['default']['NAME'] after the update code runs?
Django
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'db.sqlite3',
    }
}

# Update to PostgreSQL
DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql'
DATABASES['default']['NAME'] = 'prod_db'
DATABASES['default']['USER'] = 'admin'
A'prod_db'
Bnull
C'db.sqlite3'
D'' (empty string)
Attempts:
2 left
💡 Hint
The update changes the NAME key after initial assignment.
🔧 Debug
advanced
2:00remaining
Why does this Django project fail to connect to the database?
This Django DATABASES setting causes a connection failure. What is the most likely cause?
Django
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': '',
        'PORT': '',
    }
}
AMissing 'OPTIONS' key with SSL settings causes failure.
BEmpty 'HOST' and 'PORT' cause Django to fail connecting to MySQL server.
CUsing 'django.db.backends.mysql' requires installing a MySQL driver which might be missing.
DThe 'NAME' key must be a full file path for MySQL connections.
Attempts:
2 left
💡 Hint
Check if the database driver is installed and compatible.
🧠 Conceptual
expert
2:30remaining
How does Django handle multiple databases in settings?
If you configure multiple databases in Django's DATABASES setting, how does Django decide which database to use for a model by default?
ADjango uses the first database listed in the <code>DATABASES</code> dictionary for all models.
BDjango randomly picks one of the configured databases for each query.
CDjango requires specifying the database in each model's Meta class to select it.
DDjango uses the database named 'default' for all models unless routing is customized.
Attempts:
2 left
💡 Hint
Think about the meaning of the 'default' key in the DATABASES setting.

Practice

(1/5)
1. What is the purpose of the ENGINE setting in Django's database configuration?
easy
A. To define the path to the database file
B. To set the username for the database connection
C. To specify the type of database Django should use
D. To configure the database password

Solution

  1. Step 1: Understand the role of ENGINE

    The ENGINE setting tells Django which database backend to use, like SQLite, PostgreSQL, or MySQL.
  2. Step 2: Differentiate from other settings

    Settings like NAME, USER, and PASSWORD handle connection details, not the database type.
  3. Final Answer:

    To specify the type of database Django should use -> Option C
  4. Quick Check:

    ENGINE = database type [OK]
Hint: ENGINE always means database type, not credentials [OK]
Common Mistakes:
  • Confusing ENGINE with NAME or USER
  • Thinking ENGINE sets username or password
  • Assuming ENGINE is a file path
2. Which of the following is the correct way to configure a SQLite database in Django's settings.py?
easy
A. """DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'mydb' } }"""
B. """DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'mydb' } }"""
C. """DATABASES = { 'default': { 'ENGINE': 'sqlite3', 'NAME': 'db.sqlite3' } }"""
D. """DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3' } }"""

Solution

  1. Step 1: Identify correct ENGINE for SQLite

    Django uses django.db.backends.sqlite3 as the ENGINE for SQLite databases.
  2. Step 2: Check the NAME setting for SQLite

    The NAME should be a path to the SQLite file, commonly BASE_DIR / 'db.sqlite3' using pathlib.
  3. Final Answer:

    DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3' } } -> Option D
  4. Quick Check:

    SQLite ENGINE = django.db.backends.sqlite3 [OK]
Hint: SQLite uses django.db.backends.sqlite3 and file path NAME [OK]
Common Mistakes:
  • Using 'sqlite3' instead of 'django.db.backends.sqlite3'
  • Confusing ENGINE with PostgreSQL or MySQL backends
  • Setting NAME as just a string without path
3. Given this Django database configuration snippet, what database will Django connect to?
DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'myproject',
    'USER': 'admin',
    'PASSWORD': 'secret',
    'HOST': 'localhost',
    'PORT': '5432',
  }
}
medium
A. PostgreSQL database named 'myproject' on localhost
B. MySQL database named 'myproject' on localhost
C. SQLite database named 'myproject'
D. No database connection due to missing ENGINE

Solution

  1. Step 1: Check the ENGINE setting

    The ENGINE is set to django.db.backends.postgresql, which means Django will use PostgreSQL.
  2. Step 2: Identify database details

    The NAME is 'myproject', HOST is 'localhost', and PORT is '5432' which are typical PostgreSQL defaults.
  3. Final Answer:

    PostgreSQL database named 'myproject' on localhost -> Option A
  4. Quick Check:

    ENGINE = postgresql means PostgreSQL DB [OK]
Hint: ENGINE shows database type; PostgreSQL means option C [OK]
Common Mistakes:
  • Assuming SQLite because of 'myproject' name
  • Confusing PostgreSQL with MySQL
  • Ignoring HOST and PORT settings
4. Identify the error in this Django database configuration:
DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'mydb',
    'USER': 'root',
    'PASSWORD': 'pass123',
    'HOST': '',
    'PORT': '',
  }
}
medium
A. NAME must be a file path for MySQL
B. Empty HOST and PORT cause connection failure
C. PASSWORD cannot be empty string
D. ENGINE should be 'django.db.backends.postgresql'

Solution

  1. Step 1: Check HOST and PORT values

    HOST and PORT are empty strings, which can cause connection issues because MySQL expects valid host and port or defaults.
  2. Step 2: Understand default behavior

    Leaving HOST and PORT empty may work if MySQL uses defaults, but empty strings are not recommended and can cause errors.
  3. Final Answer:

    Empty HOST and PORT cause connection failure -> Option B
  4. Quick Check:

    HOST and PORT must be valid or omitted [OK]
Hint: Empty strings for HOST/PORT can break MySQL connection [OK]
Common Mistakes:
  • Thinking empty strings mean localhost defaults
  • Confusing ENGINE with PostgreSQL
  • Assuming PASSWORD must be non-empty always
5. You want to configure Django to use PostgreSQL in production but SQLite in development. Which approach correctly handles this in settings.py?
hard
A. Use environment variables to switch ENGINE and NAME based on environment
B. Set ENGINE to PostgreSQL always and change NAME for dev and prod
C. Use SQLite ENGINE and change USER for production
D. Configure both databases in DATABASES and Django picks automatically

Solution

  1. Step 1: Understand environment-based configuration

    Using environment variables lets you switch database settings without changing code, ideal for dev vs prod.
  2. Step 2: Why other options fail

    Always setting PostgreSQL ENGINE won't work in dev without PostgreSQL installed; changing USER alone doesn't switch DB type; Django does not auto-pick between multiple databases without explicit routing.
  3. Final Answer:

    Use environment variables to switch ENGINE and NAME based on environment -> Option A
  4. Quick Check:

    Env vars enable flexible DB config [OK]
Hint: Use env vars to switch DB settings per environment [OK]
Common Mistakes:
  • Hardcoding production DB in dev settings
  • Assuming Django auto-selects DB from multiple configs
  • Changing USER does not switch database type
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard