Sample questions
Question 1Easy
Which command runs the project's tests?
- python manage.py check
- python manage.py runtests
- python manage.py pytest
- python manage.py test
Answer: D. python manage.py test
Explanation
It discovers test*.py files.
Step-by-step solution
- Run command.
- Results printed.
Why the other options are wrong
- A. check validates configuration.
- B.
python manage.py runtests is not a Django management command; the correct choice is python manage.py test. - C. Needs a plugin.
Question 2Easy
Which signal fires right after a model's save() completes?
- after_save
- saved
- post_save
- on_commit
Answer: C. post_save
Explanation
Connect receivers with the @receiver decorator.
Step-by-step solution
- Define receiver.
- Connect to post_save.
Why the other options are wrong
- A.
after_save is not a Django signal; the correct choice is post_save. - B.
saved is not a Django signal; the correct choice is post_save. - D. Different mechanism.
Question 3Medium
Which cache API stores a value for 60 seconds?
- cache.set('k', v, 60)
- cache.put('k', v, ttl=60)
- cache.save('k', v)
- cache.add('k', 60)
Answer: A. cache.set('k', v, 60)
Explanation
cache.get returns None on a miss unless a default is given.
Step-by-step solution
- Set key.
- Timeout seconds.
Why the other options are wrong
- B.
cache.put('k', v, ttl=60) is not the cache API; the correct choice is cache.set('k', v, 60). - C.
cache.save('k', v) is not the cache API; the correct choice is cache.set('k', v, 60). - D. add has different semantics.
Question 4Medium
What does @transaction.atomic guarantee for a function?
- Faster queries, because Django batches the statements in one round trip to the DB
- All database changes inside commit together or roll back together
- Automatic retries
- Read-only mode
Answer: B. All database changes inside commit together or roll back together
Explanation
An exception rolls back the whole block.
Step-by-step solution
- Open transaction.
- Commit or rollback.
Why the other options are wrong
- A. No speed effect.
- C. No retry.
- D. Not read-only.
Question 5Medium
How do you send a test request in a Django TestCase?
- self.client.get('/url/')
- requests.get('/url/')
- urllib.get('/url/')
- self.fetch('/url/')
Answer: A. self.client.get('/url/')
Explanation
The test client simulates requests without a server.
Step-by-step solution
- Use client.
- Assert response.
Why the other options are wrong
- B. Real HTTP library, not the test client.
- C. urllib has no get() function, and it would not use the Django test client anyway.
- D. Tornado style.
Question 6Hard
What does the TestCase class in django.test do for each test?
- Drops and recreates the whole database before every single test method runs
- Shares data between tests
- Wraps it in a transaction that is rolled back, giving a clean database
- Uses the production DB
Answer: C. Wraps it in a transaction that is rolled back, giving a clean database
Explanation
Django creates a test database automatically.
Step-by-step solution
- Test starts.
- Rollback at end.
Why the other options are wrong
- A. That is slower TransactionTestCase style.
- B. Isolation is the aim.
- D. A separate test DB is used.
Question 7Hard
Why use transaction.on_commit() to send an email after saving?
- It speeds up SQL by batching all statements into one round trip to the server
- It disables rollbacks
- It encrypts data
- It runs only if the transaction commits successfully
Answer: D. It runs only if the transaction commits successfully
Explanation
Avoids sending messages for data that rolls back.
Step-by-step solution
- Schedule callback.
- Run after commit.
Why the other options are wrong
- A. No speed effect.
- B. Opposite.
- C. on_commit runs a callback after the commit; it does not encrypt anything.
Practise all 7 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →