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.
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 Django interview questions
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 pageqs = Job.objects.filter(status="open").order_by("-posted_at") # no SQL yet
first_page = list(qs[:20]) # SQL runs hereChecked 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 pagepython manage.py makemigrations jobs
python manage.py migrateChecked against: Django docs: migrations
Intermediate Django interview questions
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 pagejobs = 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 jobChecked 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 pagefrom 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 pagefrom django.views.generic import ListView
class OpenJobList(ListView):
queryset = Job.objects.filter(status="open")
paginate_by = 20Checked against: Django docs: class-based views
Advanced Django interview questions
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 pageclass 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 pagefrom 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 pageasync 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 pagepython manage.py check --deployChecked against: Django docs: settings
How to practice with this page
- 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.
Related interview prep and tools
Core Python with real output.
Open →QuestionsPostgreSQL interview questionsThe database Django projects often use.
Open →QuestionsHTML interview questionsTemplates render HTML: forms, validation and safety.
Open →QuestionsREST API interview questionsHTTP methods and status codes, with real responses.
Open →Free toolResume summary generatorDraft a summary for a Python Developer role; the title is filled in, you add your experience.
Open →Free toolLinkedIn headline generatorWrite a headline for a Python Developer search.
Open →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.
Questions people ask
What Django topics come up often in interviews?
Is Django REST Framework part of Django?
Were these snippets run?
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.
- 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.
- Django docs: asynchronous support (read 2026-11-15)
- Django docs: cache framework (read 2026-11-15)
- Django docs: class-based views (read 2026-11-15)
- Django docs: CSRF protection (read 2026-11-15)
- Django FAQ: general (read 2026-11-15)
- Django docs: middleware (read 2026-11-15)
- Django docs: migrations (read 2026-11-15)
- Django docs: models (read 2026-11-15)
- Django docs: database access optimization (read 2026-11-15)
- Django docs: QuerySet API reference (read 2026-11-15)
- Django docs: making queries (read 2026-11-15)
- Django docs: security (read 2026-11-15)
- Django docs: settings (read 2026-11-15)
- Django docs: signals (read 2026-11-15)
- Django docs: database transactions (read 2026-11-15)