Overview of Django Styleguide Core Principles
masterThe Django Styleguide provides a pragmatic, opinionated structure for building scalable Django applications by separating concerns between the 'core' (business logic) and the 'interface' (APIs, CLI, etc.).
Where to put business logic:
- Services: Functions that primarily handle writing data to the database.
- Selectors: Functions that primarily handle fetching data from the database.
- Model properties: For non-trivial, simple logic (with exceptions).
- Model
cleanmethod: For additional validations (with exceptions).
Where NOT to put business logic:
- APIs and Views
- Serializers and Forms
- Form tags
- Model
savemethod - Custom managers or querysets (use them for exposing model interfaces, not domain logic)
- Signals (use only for decoupling unrelated components or cache invalidation)
Decision Rule: Model Properties vs. Selectors
- Use a Selector if the property spans multiple relations.
- Use a Selector if the property is non-trivial and could cause
N + 1query problems when serialized.