This book will cover Django-specific design and architecture patterns, which would be useful to a Django developer. This is how each pattern will be presented:
Pattern name
The heading is the pattern name. If it is a well-known pattern, the commonly used name is used; otherwise, a terse, self-descriptive name has been chosen. Names are important, as they help in building the pattern vocabulary. All patterns will have the following parts:
- Problem: This briefly mentions the problem
- Solution: This summarizes the proposed solution(s)
- Problem Details: This elaborates the context of the problem and possibly gives an example
- Solution Details: This explains the solution(s) in general terms and provides a sample Django implementation
Despite their near universal usage, patterns have their share of criticism too. The most common arguments against them are as follows:
- Patterns compensate for the missing language features: Peter Norvig found that 16 of the 23 patterns in design patterns were invisible or simpler in dynamic languages such as Lisp or Python. For instance, as functions are already objects in Python, it would be unnecessary to create separate classes to implement strategy patterns.
- Patterns repeat best practices: Many patterns are essentially formalizations of best practices, such as separation of concerns, and could seem redundant.
- Patterns can lead to over-engineering: Implementing the pattern might be less efficient and excessive compared to a simpler solution.
Although some of the previous criticisms are quite valid, they are based on how patterns are misused. Here is some advice that can help you understand how best to use design patterns:
- Patterns are best used to communicate that you are following a well-understood design approach
- Don't implement a pattern if your language supports a direct solution
- Don't try to retrofit everything in terms of patterns
- Use a pattern only if it is the most elegant solution in your context
- Don't be afraid to create new patterns
Generally, the Python community uses the term Pythonic to describe a piece of idiomatic code. It typically refers to the principles laid out in The Zen of Python. Written like a poem, it is extremely useful to describe such a vague concept.
Furthermore, Django developers have crisply documented their design philosophies while designing the framework at https://docs.djangoproject.com/en/dev/misc/design-philosophies/.
While the document describes the thought process behind how Django was designed, it is also useful for developers using Django to build applications. Certain principles such as Don't Repeat Yourself (DRY), loose coupling, and tight cohesion can help you write more maintainable and idiomatic Django applications.
Django or Python best practices suggested by this book would be formatted in the following manner: