Here are some questions (of my taste) related to Django that I think are useful to ponder upon. They are not interview questions per se, but I think it could be a measure to reason your competence by checking yourself with each question. Some of them constitute a bigger problem and therefore require a little thinking where others could be answered immediately or dwell upon some implementation details.
If you have any suggestions, feel free to add your question via a PR, however I would only accept questions that follow the theme and are a bit qualified.
-
To improve update queries in the application, the project manager creates a task which entails replacing model
save()
calls withsave(update_fields=[...])
. After the task is completed, the client starts to complain about outdated content (i.e., seeing the same content all the time) on various endpoints. What could be the issue? -
Two developers are working on the same model in separate branches. Developer A finishes his work and his branch is merged to main. After some time, developer B finishes his work, and to make sure there were no conflicts with B, he rebases their branch from main; there appears to be no conflicts and branch of B is merged. The main branch is then submitted to development server, where the auto migrate commands fails due to conflicting migrations. What happened?
-
How would you add a unique field (say UUID) to a model, in a backwards compatible manner?
-
In the following code, what might be the reason for the usage of
transaction.atomic
?@transaction.atomic def commit(instance, data): for attr, value in data.items(): setattr(instance, attr, value) instance.save(update_fields=data.keys())
-
A developer uses a many-to-many field with
"self"
to hold "following users" of a user. After usingadd
method on the field, they realize that the target user's following list is also populated. What might have caused this? -
Could you describe a scenario in which a recursion error is propagated through signals? Assume that a signal does not directly invoke itself.
-
What is the difference between psycopg2 and psycopg2-binary? Which one should be used in which case?
-
Which library do you need to install to use
models.ImageField
? -
What is 'gunicorn'?
-
The code below produces incorrect counts for specified fields. What could be the reason?
User.objects.annotate(Count("following"), Count("followers"))
-
Explain the distinction between
FieldFile
andFileField
. -
Why do some developers advocate the use of
default=timezone.now
overauto_now_add=True
? -
How does
manage.py
determine the settings module of your application? -
What is the difference between Django-provided
TestCase
andTransactionTestCase
? -
How would you handle a multi-language Django project? Which Django tools and 3rd party libraries would you use?
-
Could you tell of some built-in security measures taken by Django? Do you know of a common security consideration, in the context of the web, that Django does not provide?
-
Why adding
null=True
to aCharField
is a bad idea? -
You are asked to build a "soft-deletion" implementation that would encompass multiple models across multiple apps. In general terms, how would you do it?
-
Say you have a field which has a
RegexValidator
. How would you impose this validation at the database level as well, without using raw SQL? -
Say you are coding in a context where it is not possible import the desired model class due to circular imports. How would you acquire this model class?
-
Other than omitting
prefetch_related
andselect_related
, what are some common mistakes developers make that slow down Django applications? -
You are assigned to monitor a Django application in production; your aim is to find possible bottlenecks and optimize overall performance of the application. What tools are you going to use? By giving example cases, what type of solutions could you implement?
-
What is 'middleware'? Can you name some middleware classes that are provided by Django, with their purpose? Could you tell of an example where you needed to use a middleware?
-
Clients complain about the slowness of the Django admin site. What could be the reasons behind this issue, and how would you improve it?
-
Can you differentiate model methods and model manager methods? In what cases using one suits better than the other?
-
One could hook up the
post_save
signal to do processing after the model instance is saved, it is also possible to overridesave()
model method to do the same work. Which method would you use in which case? -
If you omit
related_name
on aForeignKey
field, what does Django set as default? -
A developer converts
ForeignKey
withunique=True
toOneToOneField
, knowing these two correspond to the same structure in the database. Would this conversion break anything in the domain of Django? If so, how? -
In QuerySets, what is the difference between using one filter with many arguments versus chaining multiple filters?
-
What does the phrase “QuerySets are lazy” mean? Give some instances where a QuerySet would get evaluated.
-
How would you ensure an email is sent only after an object is created in the database, and how would you test this functionality?
-
Do you know how Django development team manages its releases? Which version of Django would you use at any given time, and why?
-
What do you think about generic relations? Can you tell few pros and/or cons?
-
Django is often considered to have a monolithic architecture, what does this mean?
-
Give an instance from Django APIs where operator overriding is used.
-
What does
F
object do in Django? Give a couple of distinct cases where the usage ofF
object would be appropriate. -
Explain Cross-site request forgery (CSRF) vulnerability and Django's secure implementation against it.
-
Explain BREACH attack. Does Django have any mitigation against it? How would such mitigation work?
-
Assume that you are adding a new setting, which should hold some sort of secret such as an API key. Security-wise, in what manner would you add that key so that it would be more secure i.e., not easily exposed to outside?
-
You are doing a security audit for a Django website, and by checking "Not found" page, you have realized that the website has not disabled the
DEBUG
mode. To make your point, you want to trigger a 500 error so that all the environment variables would be exposed; in which case you would send the exposed variables to the customer in joy. How would you trigger a 500 error easily, in this case? -
What are some things that make apparent that a website uses Django as backend?
-
In the context of database backup, why is the usage of Django management commands
loaddata
anddumpdata
are not desirable? What would a proper database backup setup entail? -
In summary, how does Django migrations work? Why do we need migrations? Do you know what happens in the background? What does Django migrations entail in the actual database?
-
How do you scale a Django application?
-
What is WSGI; how about ASGI?
-
Using Django, how would you transfer some data via untrusted environments (e.g., email), making sure of the authenticity and integrity of the data while receiving it?
-
What do you know about system check framework, can you give an example of a built-in check? Is it possible to write your own checks?
-
What would you do if you wanted to associate users with sessions?
-
What is a swappable dependency?
-
How does the intermediate table in a many-to-many relationship is generated, and how would you add custom fields to that model?
-
What does
CONN_MAX_AGE
setting do? -
What does
ATOMIC_REQUESTS
setting do? What might be the pros and cons for enabling atomic requests? -
Sometimes, it is preferable to lock a row in the database during certain transactions. Can you give one situation where this would be helpful? And how would you do it?
-
How would you upgrade a Postgres deployment to the next major release?
-
How do JWT tokens work? What does the phrase "stateless authentication" mean?
-
What type of authentication mechanism does Django use by default? Have you ever used alternative authentication methods?
-
During error monitoring, you realize that workers frequently shut down with
SystemExit(1)
, what could be the cause? -
What are the differences between class-based and function-based views? Which style do you use in which context?
-
How would you Dockerize a Django application?
-
What is Redis? What might be some reasons to use Redis? Do you have any concrete examples using Redis?
-
Why would one need multiple Celery workers?
-
What is the purpose of Celery beat?
-
In a Django application, the response times slows down during 3 a.m. every night, even though the traffic is roughly the same. What could be the cause?
-
How would one monitor Celery tasks?
-
Why, by default, is it not possible to pass a model instance to a Celery task? And how would you achieve this behavior?
-
What are some use cases for Celery? What types of Celery tasks did you write? Can you justify your use case?
-
Why is it not recommended to serve static files with Django?
-
How would you serve large amounts of JSON data (>5MB) via an API, in an efficient manner?
-
How would you serve a large CSV file (>5MB), in an efficient manner?
-
How do Django signals compare to database triggers? In which contexts would you prefer using triggers over signals?
-
What does
transaction.on_commit
do? Can you give an example use case for this functionality? -
Why is a bad idea to use
functools.lru_cache
on model methods? -
How would you separate development and production requirements for given Django project? (e.g., Python dependencies, settings etc.)
-
How would you adapt Django migrations for a project that went live and never had any migration files to begin with?
-
Django ORM is powerful, but it does not necessarily allow for translating every SQL statement. Can you give justified/useful SQL queries that could not be performed by only using the ORM?
-
What is your go-to route for documenting your Django project? How do you fragment your documentation, i.e., which topics do you include?
-
Which tools do you use for you project to ensure consistent code-style and formatting? How do you automate them? Any tools you use that checks for Django-specific constructs?
-
In your opinion, what is the worst part of developing a Django application?
-
What does
RunPython
migration operation do and in which cases would it be appropriate to use it? -
What does
SeparateDatabaseAndState
migration operation do and in which cases would it be appropriate to use it? -
What does
RunSQL
migration operation do and in which cases would it be appropriate to use it?