Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Django Design Patterns 
Arun Ravindran
About Me 
Arun Ravindran 
Blogs and screencasts at arunrocks.com
Outline 
●Why Design Patterns? 
●Does Django follow MVC pattern? 
●Model Patterns 
●View Patterns 
●Template Patterns 
●Gy...
“ 
A design pattern is a general reusable solution to a commonly occurring problem within a given context in software desi...
●Pattern are often elegant and beautiful code 
●Repeatable solutions 
●Gives a Common Lingo 
●Reduces Trial and Error 
●Fo...
Django and Patterns 
GoF Pattern 
Django Component 
Explanation 
Observer pattern 
Signals 
When one object changes state,...
Does Django follow MVC? 
●MVC is easily misunderstood 
●Creators of Django prefer the term MTV 
●In reality, Django is an ...
Model Patterns 
●Pattern: User Profiles 
●Pattern: Service Objects 
●Pattern: Custom Model Managers
User Profiles
User Profiles (contd.) 
1.Define the profile model 
2.Listen for User model’s post_save signal
User Profiles (contd.) 
3.Mention your app’s AppConfig 
4.Import signals in the AppConfig’s ready 
5.(Optional) Inline the...
User Profiles (contd.) 
Usage: 
UserProfile.objects.get_or_create(user=u)
Service Objects 
●The adage “Fat Models, Thin Views” can lead to frighteningly obese models. 
●Service Objects are POPOs t...
Service Objects (contd.) 
1.Your model delegates to service objects 
2.Services are specialized classes:
Custom Model Managers
Custom Model Managers (contd.) 
1.Use custom manager for common queries 
2.Add it to your model
View Patterns 
●Pattern: Context Enhancers 
●Pattern: Access Controlled Views
Detour: What is a Mixin? 
•Mixin is a class used to add properties and methods to other classes 
•Composition or Inheritan...
Context Enhancers 
●Several views might need the same queryset or values in context 
●Overriding get_context_data everywhe...
Access Controlled Views
Access Controlled Views (contd.) 
1.By using a decorator on a function based view or a class based view 
2.Or, by overridi...
Template Patterns 
●Pattern: Template Inheritance Tree 
●Pattern: Active Link
Template Inheritance Tree
Template Inheritance Tree (contd.) 
1.Build a base structure, with placeholders 
2.Similar pages are extended from base
Active Link
Active Link (contd.) 
Here is a simple template-only approach: 
1.Every template has the following line 
2.The snippet in ...
And the list goes on...
Gyaan on Patterns 
●How not to use patterns: 
oDon’t blindly apply. Explain why. 
oDon’t apply a pattern if you language s...
PATTERNS ARE EVERYWHERE
Thank you!
Image Credits 
•http://fooyoh.com/geekapolis_gadgets_wishlist/657945 
•http://www.bigalspets.ca/multi-stage- canister-filt...
Upcoming SlideShare
Loading in …5
×

Django Patterns - Pycon India 2014

2,077 views

Published on

My talk on Django Design patterns http://in.pycon.org/funnel/2014/227-django-design-patterns

Published in: Software
  • Be the first to comment

Django Patterns - Pycon India 2014

  1. 1. Django Design Patterns Arun Ravindran
  2. 2. About Me Arun Ravindran Blogs and screencasts at arunrocks.com
  3. 3. Outline ●Why Design Patterns? ●Does Django follow MVC pattern? ●Model Patterns ●View Patterns ●Template Patterns ●Gyaan on Applying Patterns
  4. 4. “ A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design
  5. 5. ●Pattern are often elegant and beautiful code ●Repeatable solutions ●Gives a Common Lingo ●Reduces Trial and Error ●Formalizes best practices, say - ○Don’t Repeat Yourself ○Separation of Concerns ○SOLID… Why Design Patterns?
  6. 6. Django and Patterns GoF Pattern Django Component Explanation Observer pattern Signals When one object changes state, all its listeners are notified and updated automatically Template Method Class based generic views Steps of an algorithm can be redefined by subclassing without changing the algorithm’s structure Fowler Pattern Django Component Explanation Active Record Django Models Encapsulates the database access, and adds domain logic on that data Identity Field Id Field Saves a database ID field in an object to maintain identity Template View Django Templates Renders into HTML by embedding markers in HTML
  7. 7. Does Django follow MVC? ●MVC is easily misunderstood ●Creators of Django prefer the term MTV ●In reality, Django is an MVC variant ●Compared to classic MVC – ●“Model” is comparable to Django’s Models, ●“View” is usually Django’s Templates ●“Controller” is the entire framework itself
  8. 8. Model Patterns ●Pattern: User Profiles ●Pattern: Service Objects ●Pattern: Custom Model Managers
  9. 9. User Profiles
  10. 10. User Profiles (contd.) 1.Define the profile model 2.Listen for User model’s post_save signal
  11. 11. User Profiles (contd.) 3.Mention your app’s AppConfig 4.Import signals in the AppConfig’s ready 5.(Optional) Inline the User model’s admin
  12. 12. User Profiles (contd.) Usage: UserProfile.objects.get_or_create(user=u)
  13. 13. Service Objects ●The adage “Fat Models, Thin Views” can lead to frighteningly obese models. ●Service Objects are POPOs that encapsulate a ‘service’ or interactions with a system. ●They are usually kept in a separate file named services.py ●Common use cases: Interactions with external services, Helper tasks, Long- running tasks
  14. 14. Service Objects (contd.) 1.Your model delegates to service objects 2.Services are specialized classes:
  15. 15. Custom Model Managers
  16. 16. Custom Model Managers (contd.) 1.Use custom manager for common queries 2.Add it to your model
  17. 17. View Patterns ●Pattern: Context Enhancers ●Pattern: Access Controlled Views
  18. 18. Detour: What is a Mixin? •Mixin is a class used to add properties and methods to other classes •Composition or Inheritance? Sort of both. TemplateView View ContextMixin Template ResponseMixin
  19. 19. Context Enhancers ●Several views might need the same queryset or values in context ●Overriding get_context_data everywhere isn’t DRY ●Instead, use a view mixin:
  20. 20. Access Controlled Views
  21. 21. Access Controlled Views (contd.) 1.By using a decorator on a function based view or a class based view 2.Or, by overriding the dispatch method through a mixin
  22. 22. Template Patterns ●Pattern: Template Inheritance Tree ●Pattern: Active Link
  23. 23. Template Inheritance Tree
  24. 24. Template Inheritance Tree (contd.) 1.Build a base structure, with placeholders 2.Similar pages are extended from base
  25. 25. Active Link
  26. 26. Active Link (contd.) Here is a simple template-only approach: 1.Every template has the following line 2.The snippet in _navbar.html will be: Custom tags can also be used
  27. 27. And the list goes on...
  28. 28. Gyaan on Patterns ●How not to use patterns: oDon’t blindly apply. Explain why. oDon’t apply a pattern if you language supports a direct solution oDon’t try to retro-fit everything in terms of patterns oDon’t be afraid to create new patterns.
  29. 29. PATTERNS ARE EVERYWHERE
  30. 30. Thank you!
  31. 31. Image Credits •http://fooyoh.com/geekapolis_gadgets_wishlist/657945 •http://www.bigalspets.ca/multi-stage- canister-filter-c-360.html •https://wrapbootstrap.com/theme/sanoor- navbar-WB037R7G3 •Monty Python and the Holy Grail •Warner Bros

×