Skip to content
TryHired.
Interview prep · Technical · checked against the official docs

Django interview questions: answers checked against the docs

The Python web framework questions back-end developers get, answered in plain words and checked against the Django documentation.

Updated Nov 16, 2026 · Facts checked against the official documentation · questions: TryHired
Quick answer

Django interviews cover MTV and the request cycle, QuerySet laziness, the N+1 problem with select_related and prefetch_related, migrations, middleware, CSRF, XSS and SQL injection protection, transactions, class-based views, model inheritance, signals, caching, async support and deployment checks. Below are 13 questions; each answer lists the Django 6.1 documentation pages it was checked against.

Basic

Basic Django interview questions

Advice

Basic1. How is a Django project structured, and what is MTV?

Django follows model-template-view (MTV): models define the data and talk to the database through the ORM, views hold the request-handling logic and return responses, and templates render HTML. The URL configuration routes each request to a view. It maps onto MVC, but Django's view plays the controller's role and the template plays the view's.

Checked against: Django FAQ: general

Basic2. What does it mean that QuerySets are lazy?

Building a QuerySet, including chaining filter, exclude and order_by, runs no SQL. Django runs the query when the QuerySet is evaluated: iterated, sliced with a step, or passed to list(), len() or bool(); the results are then cached on that QuerySet, so iterating it again does not query again. count() and exists() run their own query without filling that cache.

Python (Django) snippet · illustrative, not run on this page
qs = Job.objects.filter(status="open").order_by("-posted_at")   # no SQL yet
first_page = list(qs[:20])                                         # SQL runs here

Checked against: Django docs: QuerySet API reference, Django docs: making queries

Basic3. How do migrations work?

makemigrations compares your models with the existing migration files and writes new migration files describing the changes; migrate applies unapplied migrations to the database. Commit migration files with your code, because they are effectively version control for the schema. Data migrations use RunPython to change data alongside the schema.

Shell snippet · illustrative, not run on this page
python manage.py makemigrations jobs
python manage.py migrate

Checked against: Django docs: migrations

Intermediate

Intermediate Django interview questions

Advice

Intermediate4. What is the N+1 query problem, and how do select_related and prefetch_related fix it?

Looping over objects and touching a related object on each one triggers one query per object. select_related follows foreign keys and one-to-one relations with a SQL join, so the related objects come back in the same query. prefetch_related runs one extra query per relation and joins the results in Python, which also works for many-to-many and reverse relations. Django 6.1 also adds fetch modes: FETCH_PEERS batch-loads a related field for all objects from the same query the first time it is accessed.

Python (Django) snippet · illustrative, not run on this page
jobs = Job.objects.select_related("company").prefetch_related("skills")
for job in jobs:
    print(job.company.name, [s.name for s in job.skills.all()])   # no extra query per job

Checked against: Django docs: database access optimization, Django docs: QuerySet API reference

Intermediate5. What is middleware?

Middleware is a chain of hooks around every request and response: each component can inspect or change the request on the way in and the response on the way out, for example for sessions, authentication, security headers or CSRF checks. Order in the MIDDLEWARE setting matters, because some components depend on others having run first.

Checked against: Django docs: middleware

Intermediate6. How does Django protect against CSRF, XSS and SQL injection?

CSRF: the CSRF middleware checks a secret token on unsafe requests such as POST, and forms include it with the csrf_token template tag. XSS: templates escape variables by default, so the risk comes from marking content safe (mark_safe, the safe filter) or turning autoescape off. SQL injection: QuerySets build parameterized queries; raw SQL must pass user input as parameters, never by string formatting.

Django template snippet · illustrative, not run on this page
<form method="post">
  {% csrf_token %}
  {{ form.as_p }}
</form>

Checked against: Django docs: CSRF protection, Django docs: security

Intermediate7. How do transactions work in Django?

By default Django runs in autocommit mode: each query is committed immediately. transaction.atomic() wraps a block in a transaction that commits if the block succeeds and rolls back on an exception; nested atomic blocks use savepoints. ATOMIC_REQUESTS wraps every request in a transaction when you need that globally.

Python (Django) snippet · illustrative, not run on this page
from django.db import transaction

with transaction.atomic():
    order = Order.objects.create(customer=customer)
    Payment.objects.create(order=order, amount=total)

Checked against: Django docs: database transactions

Intermediate8. When do you use function-based views versus class-based views?

Function-based views are plain functions: explicit and easy to follow for simple or unusual logic. Class-based views reuse behavior through inheritance and mixins, and the generic views (ListView, DetailView, CreateView and others) cover common patterns with little code. Pick whichever keeps a given view clearest.

Python (Django) snippet · illustrative, not run on this page
from django.views.generic import ListView

class OpenJobList(ListView):
    queryset = Job.objects.filter(status="open")
    paginate_by = 20

Checked against: Django docs: class-based views

Advanced

Advanced Django interview questions

Advice

Advanced9. What are the three kinds of model inheritance?

Abstract base classes share fields and methods without creating a table of their own; each child gets its own table. Multi-table inheritance gives every model its own table, joined by a one-to-one link, which costs a join. Proxy models change only Python behavior, such as default ordering or methods, on the same table.

Python (Django) snippet · illustrative, not run on this page
class TimeStamped(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    class Meta:
        abstract = True

class Job(TimeStamped):
    title = models.CharField(max_length=200)

Checked against: Django docs: models

Advanced10. What are signals, and when should you avoid them?

Signals let decoupled code react to events, such as post_save on a model, without the sender knowing about the receivers. They help when a separate app must react to events in another, but they hide control flow and run synchronously inside the request. When the code is in your own app, calling a function explicitly is usually clearer.

Checked against: Django docs: signals

Advanced11. How do you use caching in Django?

Django's cache framework works at several levels: the whole site through middleware, individual views with cache_page, template fragments, and the low-level cache API for any value. Backends include Memcached, Redis, the database and local memory. A cache is for speed, not storage: anything in it can disappear.

Python (Django) snippet · illustrative, not run on this page
from django.views.decorators.cache import cache_page

@cache_page(60 * 15)
def job_list(request):
    ...

Checked against: Django docs: cache framework

Advanced12. Does Django support async?

Yes. Django supports async views, and under an ASGI server they can handle long-running requests efficiently. Async views also run under WSGI, with a small adaptation cost per request and without that efficiency. Calling synchronous code from async views, including large parts of the ORM, goes through adapters such as sync_to_async.

Python (Django) snippet · illustrative, not run on this page
async def health(request):
    return JsonResponse({"status": "ok"})

Checked against: Django docs: asynchronous support

Advanced13. What do you check before deploying Django to production?

DEBUG = False together with a correct ALLOWED_HOSTS; a secret key loaded from the environment rather than code; HTTPS with secure cookie settings; static files served efficiently; and a production database and cache. Run python manage.py check --deploy, which reports common misconfigurations.

Shell snippet · illustrative, not run on this page
python manage.py check --deploy

Checked against: Django docs: settings

Practice

How to practice with this page

Advice
  • Turn on query logging and fix an N+1 problem in a small project.
  • Explain each security protection and how a developer could switch it off by mistake.
  • Build one view as a function and as a class-based view, and compare.
  • Prepare for Django REST Framework questions if the role builds APIs.
Next

Related interview prep and tools

Disclosure: TryHired makes HIRED, a paid job-search program, so we have a conflict of interest when we mention it. This question bank is free, with no TryHired signup.

FAQ

Questions people ask

What Django topics come up often in interviews?
MTV and the request cycle, the ORM and QuerySet laziness, N+1 queries with select_related and prefetch_related, migrations, middleware, authentication, security protections, transactions, class-based views, caching and, for API roles, Django REST Framework.
Is Django REST Framework part of Django?
No. Django REST Framework is a separate package built on Django for building APIs. Interviews for API roles often cover it alongside core Django: serializers, viewsets, routers, authentication and permissions.
Were these snippets run?
No. Django is not installed on the build machine, so the snippets are illustrations and no output is shown. They assume a project with the models named. The answers follow the Django 6.1 documentation; features such as fetch modes are new in 6.1.
Method

How this page is built

TryHired wrote these questions and answers. Statements about the technology were checked against the official documentation pages listed below, saved when the page was built (2026-11-16). The code snippets are short illustrations and were not run on this page. The question list is general practice: it does not predict what a particular employer will ask.

What this page can't tell you
  • Interviewers ask different questions for different roles and levels.
  • Answers are short explanations, not complete references.
  • No Django code was run on this page. The answers follow the Django 6.1 documentation; features such as fetch modes do not exist in older versions.
Sources
  1. Django docs: asynchronous support (read 2026-11-15)
  2. Django docs: cache framework (read 2026-11-15)
  3. Django docs: class-based views (read 2026-11-15)
  4. Django docs: CSRF protection (read 2026-11-15)
  5. Django FAQ: general (read 2026-11-15)
  6. Django docs: middleware (read 2026-11-15)
  7. Django docs: migrations (read 2026-11-15)
  8. Django docs: models (read 2026-11-15)
  9. Django docs: database access optimization (read 2026-11-15)
  10. Django docs: QuerySet API reference (read 2026-11-15)
  11. Django docs: making queries (read 2026-11-15)
  12. Django docs: security (read 2026-11-15)
  13. Django docs: settings (read 2026-11-15)
  14. Django docs: signals (read 2026-11-15)
  15. Django docs: database transactions (read 2026-11-15)