This presentation is a part of Sunscrapers' weekly talks. Michał presents and evaluates different approaches to organising business logic code in a Django Project.
Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...
Fat query sets and Skinny Models
1. sunscrapers.comYour favored tech partner.
Thick QuerySets and Thin Models, or
Where to Put Business Logic in
Django
Michał Nakoneczny
2017-05-19
2. sunscrapers.comYour favored tech partner.
A layer which encodes the real-world business rules that
determine how data can be created, displayed, stored, and
changed.
Business Logic Layer
11. sunscrapers.comYour favored tech partner.
Pros:
● Testable;
● Readable?;
● DRY
Cons:
● Unstructured in a large code base;
● Can lead to unoptimised solutions.
Idea 3: Services
13. sunscrapers.comYour favored tech partner.
BL: Approving and Publishing
class PostQuerySet(QuerySet):
def approve(self):
return self.update(status=Post.STATUS_APPROVED)
def publish(self):
return self.filter(status=Post.STATUS_APPROVED).update(
status=Post.STATUS_PUBLISHED)
class Post(Model):
...
objects = PostQuerySet.as_manager()
14. sunscrapers.comYour favored tech partner.
Pros:
● Testable;
● Readable;
● DRY;
● Optimised (unless you do something stupid).
Cons:
● Checking Business Logic rules is more complicated.
● Integrations get more difficult.
Idea 4: QuerySets/Managers