Interview questions

Django Interview Questions

20 Django interview questions on the ORM, querysets, views, middleware, security and performance, with model answers for candidates and interviewers.

Django Interview Questions

What's here

20 Django interview questions, grouped into 4 areas: orm and querysets, request flow and architecture, security, configuration and deployment and performance and practical scenarios. Each one comes with why it is worth asking and what a strong answer contains, so the same question can be scored the same way by different interviewers.

  • Give a short view that loops over objects and touches a related field, then ask how many queries it runs; it tests ORM understanding faster than definitions
  • Ask the candidate to explain a past performance fix with before-and-after evidence
  • Probe security answers with a follow-up such as 'when would you use csrf_exempt?' to see whether knowledge goes beyond the default
For hiring teams

Cohesyve finds, contacts and tests Django candidates on the real work, then ranks them for these interviews. 14-day free trial for 10 candidates, not charged until it ends.

Preparing for this interview yourself? Practise it free →

Django is a Python web framework used for everything from content sites to large APIs. These Django interview questions test whether a developer understands the ORM and how querysets hit the database, how requests flow through views and middleware, how Django's built-in security protections work and how to keep an application fast. The 'what to look for' notes give the substance of a strong answer, so interviewers can score consistently and candidates can check their understanding.

ORM and QuerySets

Most Django performance problems and many bugs start in the ORM. These questions test how well the candidate understands what SQL their code produces.

  1. 1

    QuerySets in Django are described as lazy. What does that mean in practice?

    Why ask it

    A basic concept that explains many surprises around query counts and caching.

    What to look for

    Building or filtering a QuerySet does not touch the database; the query runs when the QuerySet is evaluated, for example by iterating, calling list(), len() or bool(), or slicing with a step. Strong answers mention that an evaluated QuerySet caches its results, so reusing the same object avoids a second query while re-filtering creates a new one.
  2. 2

    What is the difference between select_related and prefetch_related? When do you use each?

    Why ask it

    The most common Django ORM performance question.

    What to look for

    select_related follows single-valued relationships (foreign key and one-to-one) with a SQL join in the same query. prefetch_related runs a separate query per relationship and joins the results in Python, which works for many-to-many and reverse foreign key relations. Good candidates explain they reach for these to fix N+1 query patterns.
  3. 3

    How would you find and fix an N+1 query problem in a Django view?

    Why ask it

    Tests practical debugging, not just definitions.

    What to look for

    Spotting it with a query-logging tool or by counting queries in tests, finding the loop that touches a related object per row, then adding select_related or prefetch_related (or annotations for counts). Strong answers include adding a test that asserts the query count so the problem does not come back.
  4. 4

    Two requests increment the same counter field at the same time and one update is lost. Why, and how do you fix it?

    Why ask it

    Checks understanding of race conditions in ORM code.

    What to look for

    The read-modify-write in Python lets the second save overwrite the first. Using an F() expression (for example update(count=F('count') + 1)) makes the database do the increment based on its current value. Candidates may also mention select_for_update inside a transaction for more complex cases.
  5. 5

    When would you use defer() or only(), and what is the risk?

    Why ask it

    Separates candidates who know the API from those who understand its trade-offs.

    What to look for

    They limit which fields load, which can help with large text or JSON columns. The risk is that accessing a deferred field later triggers an extra query per object, recreating an N+1 problem. Django's own docs treat them as advanced optimisations to use after other options.
For hiring teams

Before the Django interviews, see who can do the work

Cohesyve finds and contacts Django candidates from your job description and tests each one on an AI job simulation of the role, so these questions go to a ranked shortlist. 14-day free trial for 10 candidates, not charged until it ends.

Request Flow and Architecture

How Django handles a request and how the candidate structures an application.

  1. 6

    Django is often called an MVC framework. How does its own documentation describe the split?

    Why ask it

    Checks the candidate's mental model of where logic belongs.

    What to look for

    Django describes itself as MTV: model, template, view. The view is the Python callable for a URL and decides which data is shown; the template decides how it looks; the framework's URL routing acts as the controller. Strong answers say where business logic should live (models or a service layer, not templates).
  2. 7

    Walk me through what happens between a browser request and the response in Django.

    Why ask it

    Tests end-to-end understanding.

    What to look for

    The request passes through middleware in order, the URL resolver matches a pattern to a view, the view runs (often querying models and rendering a template or serialising data), and the response passes back through middleware in reverse. Mentions of the request object, context and response classes show depth.
  3. 8

    What is middleware, and give an example of when you would write your own.

    Why ask it

    A common extension point that candidates often misuse.

    What to look for

    Code that wraps every request and response, used for cross-cutting concerns: adding headers, request IDs for logging, timing, tenant detection. Good answers note that order in the MIDDLEWARE setting matters and that heavy logic in middleware slows every request.
  4. 9

    Function-based views or class-based views: how do you choose?

    Why ask it

    Reveals judgement and familiarity with both styles.

    What to look for

    Function views are explicit and easy to read for simple or unusual logic; class-based and generic views reduce repetition for standard CRUD through mixins and inheritance. A sensible candidate uses both and avoids deep mixin chains nobody can follow.
  5. 10

    What are signals, and when would you avoid them?

    Why ask it

    Signals are easy to overuse.

    What to look for

    A way to run code when events happen (for example post_save). They suit decoupled side effects, but hidden control flow makes code harder to trace and test. Strong candidates often prefer explicit calls in a service function and keep signals for integration points.

Security, Configuration and Deployment

Django ships with protections that only work if developers understand them, and production changes need care.

  1. 11

    How does Django protect against cross-site request forgery?

    Why ask it

    A standard security question with a concrete answer.

    What to look for

    CsrfViewMiddleware sets a CSRF cookie and checks that requests using unsafe methods (anything other than GET, HEAD, OPTIONS or TRACE) carry a matching token; templates add it with the csrf_token tag in POST forms. Good answers mention that views should not change state on GET, and that csrf_exempt needs a strong reason.
  2. 12

    How does the ORM help prevent SQL injection, and where can you reintroduce the risk?

    Why ask it

    Tests whether the candidate knows the escape hatches.

    What to look for

    ORM queries pass values as parameters rather than building SQL strings. The risk returns with raw(), extra() or cursor.execute() when user input is formatted into the SQL string; the safe pattern passes parameters separately.
  3. 13

    What settings do you check before deploying a Django project to production?

    Why ask it

    Practical readiness for real deployments.

    What to look for

    DEBUG off, a secret key loaded from the environment and never committed, ALLOWED_HOSTS set, HTTPS and secure cookie settings, static files served properly, database credentials outside code, logging configured. Mention of Django's deployment checklist command is a plus.
  4. 14

    How do you manage settings for local, staging and production environments?

    Why ask it

    Configuration mistakes cause outages and leaks.

    What to look for

    Environment variables or a settings module per environment with shared defaults, secrets kept out of the repository, and the same code running in every environment. Candidates should avoid environment-specific logic scattered through the codebase.
  5. 15

    How do you write and run migrations safely on a busy production database?

    Why ask it

    Schema changes are a frequent source of downtime.

    What to look for

    Reviewing generated migrations, separating schema and data migrations, adding nullable columns before backfilling, avoiding long table locks, and deploying code that works with both old and new schemas during the change. Testing migrations against a copy of production-sized data is a strong signal.

Performance and Practical Scenarios

Scenarios that mirror real Django work on growing applications.

  1. 16

    An admin list page for a large table has become very slow. How do you investigate?

    Why ask it

    A realistic problem that touches several skills.

    What to look for

    Measuring queries first, then list_select_related or get_queryset with select_related for foreign keys shown in columns, avoiding expensive computed columns, adding database indexes for filters and ordering, and limiting or replacing costly count queries on very large tables.
  2. 17

    How do you handle slow work such as sending emails or generating reports without blocking the request?

    Why ask it

    Background processing is part of most production Django apps.

    What to look for

    Moving the work to a task queue (Celery or similar is a common answer), returning quickly to the user, making tasks idempotent and retry-safe, and passing IDs to tasks instead of model instances that may go stale.
  3. 18

    How would you add caching to a Django application, and what can go wrong?

    Why ask it

    Tests understanding of caching trade-offs.

    What to look for

    Options at several levels (per-view, template fragment, low-level cache API) backed by a shared cache store in production, plus clear invalidation rules. Risks include stale data, caching per-user content for everyone, and cache stampedes.
  4. 19

    How would you build a JSON API with Django?

    Why ask it

    Most Django roles now involve APIs.

    What to look for

    Django REST framework (or a similar library) with serializers, viewsets or API views, authentication and permissions, pagination and throttling. Strong candidates talk about validation, consistent error responses and versioning.
  5. 20

    How do you test a Django application?

    Why ask it

    Testing discipline separates maintainable codebases from fragile ones.

    What to look for

    Unit tests for models and services, view or API tests with the test client, factories or fixtures for data, tests that assert query counts on critical pages, and running tests in CI. They should explain what they choose not to test.

Running the interview well

  • 1Give a short view that loops over objects and touches a related field, then ask how many queries it runs; it tests ORM understanding faster than definitions
  • 2Ask the candidate to explain a past performance fix with before-and-after evidence
  • 3Probe security answers with a follow-up such as 'when would you use csrf_exempt?' to see whether knowledge goes beyond the default
  • 4Include a migration scenario if the role touches production data; it is where many incidents start
  • 5Let candidates name third-party libraries they use, then ask why they chose them over alternatives
  • 6Score clear explanations of trade-offs above memorised API names

Common questions

What Django topics come up most in interviews?

The ORM (querysets, select_related and prefetch_related, N+1 queries), models and migrations, views and URL routing, middleware, templates, forms, authentication, CSRF and other security settings, caching, background tasks and testing. API roles add Django REST framework.

How should I prepare for a Django interview?

Build or revisit a small project and be ready to explain its models, queries and views. Practise reading code and saying how many queries it runs. Review the security settings you would check before deploying, and prepare one example of a performance problem you fixed.

Do Django interviews include Python questions?

Usually. Expect general Python questions on data structures, comprehensions, decorators, generators and exceptions, because Django code is Python code. Senior roles may add system design and database questions.

Is Django MVC or MTV?

Django's own FAQ says it is better described as MTV: model, template and view. The view chooses which data is presented, the template controls how it looks, and the framework's URL routing plays the controller role.

What is the difference between Django and Flask?

Django is a full-featured framework with an ORM, admin, authentication and forms built in. Flask is a smaller core where you choose libraries for those pieces. Interviewers mostly ask this to see whether you can explain trade-offs for a given project.

What is the N+1 query problem in Django?

It happens when code fetches a list of objects with one query and then runs another query for each object to load a related record. The fix is usually select_related for foreign keys and one-to-one relations, or prefetch_related for many-to-many and reverse relations.

For hiring teams

Save these questions for the Django candidates who can do the work

Cohesyve takes your Django job description, finds candidates, reaches out to them and runs an AI job simulation of the real work, then returns a ranked shortlist.

1,500+

assessments completed

50%

faster time-to-hire

5 min

from JD to assessment

14-day free trial · 10 candidates · Not charged until the trial ends

For job seekers

Preparing for a Django interview yourself? Practise on an AI job simulation of the role. 3-day free trial, cancel anytime.

Practise a Django interview free →

See Cohesyve in action

Free 30-min walkthrough

See it on your role