CodeOath
← All posts
Python70 min total · 18 parts

Django Fundamentals: MVT Architecture, the ORM, and Middleware

Contents — Part 18 of 18: Common Mistakes Worth Remembering
Part 18 of 18 · ~1 min

Common Mistakes Worth Remembering

  • The N+1 query problem — by far the most common Django performance bug, and an easy one to introduce without noticing since each query individually looks fine.
  • Forgetting to run makemigrations after a model change, then wondering why migrate has nothing to apply.
  • Putting business logic in templates instead of views — templates should stay close to presentation-only.
  • Using Model.objects.get() when a matching row might not exist, without catching DoesNotExist — an unhandled 500 instead of a clean 404.
  • Defaulting every ForeignKey to on_delete=CASCADE without considering whether cascading deletes are actually the intended behavior for that relationship.
  • Forgetting {% csrf_token %} on a POST form, then being confused by a 403.
  • Placing custom middleware in the wrong order — e.g., before the SessionMiddleware or AuthenticationMiddleware it actually depends on.
  • Overusing signals for logic that would be clearer, and easier to trace, living directly in a view or model method.

Python's language-level fundamentals (the ones that show up independent of any framework) are covered in Python Fundamentals for Interviews. Practice querysets, views, and the N+1 fix as runnable exercises in the code lab.