Django Django 3 — Questions and Answers
Question 1: What does the related_name argument on a ForeignKey control?
- The reverse accessor name from the related model (Correct answer)
- The database column name
- The on_delete behavior
- The field's verbose name
Correct answer: The reverse accessor name from the related model
related_name sets the attribute used to access the relationship backward from the related object.
Question 2: Which on_delete option deletes child rows when the referenced parent is deleted?
- CASCADE (Correct answer)
- PROTECT
- SET_NULL
- DO_NOTHING
Correct answer: CASCADE
models.CASCADE removes objects that reference a deleted parent record.
Question 3: What is the purpose of Django's select_related() method?
- Reduce queries by SQL-joining foreign key relations (Correct answer)
- Filter a queryset by related fields
- Order results by a related field
- Cache a queryset in Redis
Correct answer: Reduce queries by SQL-joining foreign key relations
select_related performs a SQL join to fetch related objects in one query, avoiding N+1 problems.
Question 4: In Django REST framework, what class is commonly used to convert model instances to JSON?
- Serializer (Correct answer)
- Renderer
- Validator
- Paginator
Correct answer: Serializer
Serializers translate complex types like model instances into native Python types for JSON rendering.
Question 5: Which file maps URL patterns to views in a Django app?
- urls.py (Correct answer)
- views.py
- settings.py
- apps.py
Correct answer: urls.py
urls.py defines urlpatterns that route incoming requests to view functions or classes.
Question 6: What does Django's QuerySet method values() return?
- Dictionaries instead of model instances (Correct answer)
- A single column as a flat list
- Only the primary keys
- A count of rows
Correct answer: Dictionaries instead of model instances
values() returns a QuerySet of dictionaries, each mapping field names to values.
Question 7: Which command opens an interactive Python shell with Django's settings loaded?
- python manage.py shell (Correct answer)
- python manage.py console
- python manage.py repl
- python manage.py runpython
Correct answer: python manage.py shell
manage.py shell launches an interactive interpreter configured with the project's settings.
What does the related_name argument on a ForeignKey control?