All interview guides

Django Interview Questions and Answers

24 questions that come up in Django technical interviews, each with the answer and an explanation of why it is right.

Test yourself — 90 question bank

1. What does Django follow as its architectural pattern?

Beginner

Answer: MVT (Model-View-Template)

Django follows the MVT (Model-View-Template) pattern where Models define data structure, Views handle business logic, and Templates handle presentation.

2. What is the difference between filter() and get()?

Intermediate

Answer: filter() returns QuerySet, get() returns single object

filter() returns a QuerySet (even if one result), while get() returns a single object and raises DoesNotExist if not found or MultipleObjectsReturned if multiple found.

3. What is Django REST Framework (DRF)?

Advanced

Answer: Powerful toolkit for building Web APIs

DRF is a powerful and flexible toolkit for building Web APIs in Django. Includes serializers, viewsets, routers, authentication, and more.

4. Which command creates a new Django project?

Beginner

Answer: django-admin startproject

`django-admin startproject projectname` creates a new Django project with the necessary directory structure and configuration files.

5. What are DRF serializers?

Advanced

Answer: All of the above

Serializers handle conversion between complex types (models, QuerySets) and Python/JSON data. They also provide validation similar to Django Forms.

6. What are Django signals?

Intermediate

Answer: Event notifications for decoupled applications

Signals allow certain senders to notify receivers when actions occur. Common signals: pre_save, post_save, pre_delete, post_delete.

7. Which file contains the main settings for a Django project?

Beginner

Answer: settings.py

settings.py contains all configuration settings including database configuration, installed apps, middleware, templates, and more.

8. What is the purpose of ViewSets in DRF?

Advanced

Answer: Combine logic for multiple related views into one class

ViewSets combine list, create, retrieve, update, and destroy operations into a single class. Used with routers for automatic URL configuration.

9. What is the purpose of Django middleware?

Intermediate

Answer: To process requests and responses globally

Middleware is a framework of hooks into Django request/response processing. Each middleware component processes requests before views and responses after.

10. What is database query optimization with select_related and prefetch_related?

Advanced

Answer: Both b and c

N+1 problem occurs when querying related objects in a loop. select_related (JOINs) and prefetch_related (separate queries) solve this by loading related data upfront.

11. What command creates a new Django app within a project?

Beginner

Answer: Both a and c

Both `django-admin startapp appname` and `python manage.py startapp appname` create a new Django app with models, views, and other files.

12. Which method is used for bulk creation of objects?

Intermediate

Answer: Model.objects.bulk_create()

bulk_create() efficiently inserts multiple objects in a single query. It bypasses save() method and signals for performance.

13. Which Django component defines the database structure?

Beginner

Answer: Models

Models define the database structure as Python classes. Each model class represents a database table, and class attributes represent fields.

14. What is a ForeignKey in Django models?

Intermediate

Answer: A many-to-one relationship

ForeignKey creates a many-to-one relationship. Each instance can reference one instance of another model. Requires on_delete parameter.

15. What is Django database transaction management?

Advanced

Answer: Both b and c

Transactions ensure database operations are atomic (all or nothing). Use transaction.atomic() as decorator or context manager to group operations.

16. What command applies database migrations?

Beginner

Answer: python manage.py migrate

`python manage.py migrate` applies pending migrations to the database. `makemigrations` creates migration files based on model changes.

17. What does on_delete=models.CASCADE mean?

Intermediate

Answer: Deletes related objects when referenced object is deleted

CASCADE automatically deletes related objects when the referenced object is deleted. Other options: PROTECT, SET_NULL, SET_DEFAULT, DO_NOTHING.

18. What are Django database indexes?

Advanced

Answer: Both b and c

Indexes speed up database lookups at the cost of slower writes. Add with db_index=True on fields or Meta.indexes for composite indexes.

19. What is the difference between null=True and blank=True?

Intermediate

Answer: null is database-level, blank is validation-level

null=True allows NULL in database. blank=True allows empty values in forms/validation. For text fields, use blank=True with default="" instead of null=True.

20. What is Django Celery integration for?

Advanced

Answer: Both b and c

Celery enables asynchronous task execution, background jobs, and periodic tasks. Essential for long-running operations like email sending, report generation.

21. What are Django class-based views (CBVs)?

Intermediate

Answer: All of the above

Class-based views provide an alternative to function views using Python classes. They enable code reuse through inheritance and provide generic views like ListView, DetailView.

22. What is the purpose of manage.py?

Beginner

Answer: Command-line utility for administrative tasks

manage.py is a command-line utility that provides various administrative commands like runserver, migrate, createsuperuser, and more.

23. What are Django custom template tags?

Advanced

Answer: Both b and c

Custom template tags extend template functionality. Simple tags return values, inclusion tags render templates. Defined in app/templatetags/ directory.

24. What is Django security middleware?

Advanced

Answer: Both b and c

Django includes middleware for CSRF protection, XSS prevention (template escaping), clickjacking (X-Frame-Options), and SQL injection prevention (ORM).

Ready to test yourself?

The full Django bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.

Take the Django quiz