SlideShare a Scribd company logo
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 
●Gyaan on Applying Patterns
“ 
A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design
●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?
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
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
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 model’s admin
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 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
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 Inheritance? Sort of both. 
TemplateView 
View 
ContextMixin 
Template 
ResponseMixin
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:
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 overriding the dispatch method through a mixin
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 _navbar.html will be: 
Custom tags can also be used
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 supports a direct solution 
oDon’t try to retro-fit everything in terms of patterns 
oDon’t be afraid to create new patterns.
PATTERNS ARE EVERYWHERE
Thank you!
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

More Related Content

What's hot

AngularJS: Overview & Key Features
AngularJS: Overview & Key FeaturesAngularJS: Overview & Key Features
AngularJS: Overview & Key Features
Mohamad Al Asmar
 
5 angularjs features
5 angularjs features5 angularjs features
5 angularjs features
Alexey (Mr_Mig) Migutsky
 
Factory Method Design Pattern
Factory Method Design PatternFactory Method Design Pattern
Factory Method Design Pattern
melbournepatterns
 
AngularJS introduction
AngularJS introductionAngularJS introduction
AngularJS introduction
Tania Gonzales
 
Web development with django - Basics Presentation
Web development with django - Basics PresentationWeb development with django - Basics Presentation
Web development with django - Basics Presentation
Shrinath Shenoy
 
Django Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python DevelopersDjango Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python Developers
Rosario Renga
 
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Phpfunkatron
 
Nicolas Embleton, Advanced Angular JS
Nicolas Embleton, Advanced Angular JSNicolas Embleton, Advanced Angular JS
Nicolas Embleton, Advanced Angular JS
JavaScript Meetup HCMC
 
Angular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete Course
EPAM Systems
 
Web application development with Django framework
Web application development with Django frameworkWeb application development with Django framework
Web application development with Django framework
flapiello
 
Making Django and NoSQL Play Nice
Making Django and NoSQL Play NiceMaking Django and NoSQL Play Nice
Making Django and NoSQL Play Nice
Alex Gaynor
 
AngularJS application architecture
AngularJS application architectureAngularJS application architecture
AngularJS application architecture
Gabriele Falace
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP
Rick Ogden
 
Factory method pattern
Factory method patternFactory method pattern
Factory method pattern
Shahriar Iqbal Chowdhury
 
Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular js
Aayush Shrestha
 
Struts2
Struts2Struts2
Struts2
Rajiv Gupta
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
colinkingswood
 
AngularJs presentation
AngularJs presentation AngularJs presentation
AngularJs presentation
Phan Tuan
 
Front end development with Angular JS
Front end development with Angular JSFront end development with Angular JS
Front end development with Angular JS
Bipin
 

What's hot (20)

Extjs
ExtjsExtjs
Extjs
 
AngularJS: Overview & Key Features
AngularJS: Overview & Key FeaturesAngularJS: Overview & Key Features
AngularJS: Overview & Key Features
 
5 angularjs features
5 angularjs features5 angularjs features
5 angularjs features
 
Factory Method Design Pattern
Factory Method Design PatternFactory Method Design Pattern
Factory Method Design Pattern
 
AngularJS introduction
AngularJS introductionAngularJS introduction
AngularJS introduction
 
Web development with django - Basics Presentation
Web development with django - Basics PresentationWeb development with django - Basics Presentation
Web development with django - Basics Presentation
 
Django Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python DevelopersDjango Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python Developers
 
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Php
 
Nicolas Embleton, Advanced Angular JS
Nicolas Embleton, Advanced Angular JSNicolas Embleton, Advanced Angular JS
Nicolas Embleton, Advanced Angular JS
 
Angular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete Course
 
Web application development with Django framework
Web application development with Django frameworkWeb application development with Django framework
Web application development with Django framework
 
Making Django and NoSQL Play Nice
Making Django and NoSQL Play NiceMaking Django and NoSQL Play Nice
Making Django and NoSQL Play Nice
 
AngularJS application architecture
AngularJS application architectureAngularJS application architecture
AngularJS application architecture
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP
 
Factory method pattern
Factory method patternFactory method pattern
Factory method pattern
 
Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular js
 
Struts2
Struts2Struts2
Struts2
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
AngularJs presentation
AngularJs presentation AngularJs presentation
AngularJs presentation
 
Front end development with Angular JS
Front end development with Angular JSFront end development with Angular JS
Front end development with Angular JS
 

Similar to Django Patterns - Pycon India 2014

Design pattern in android
Design pattern in androidDesign pattern in android
Design pattern in androidJay Kumarr
 
Design patterns
Design patternsDesign patterns
Design patterns
mudabbirwarsi
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
Knoldus Inc.
 
Introduction to django framework
Introduction to django frameworkIntroduction to django framework
Introduction to django framework
Knoldus Inc.
 
Design Patterns every Android developer should know
Design Patterns every Android developer should knowDesign Patterns every Android developer should know
Design Patterns every Android developer should know
muratcanbur
 
Design patterns in android
Design patterns in androidDesign patterns in android
Design patterns in android
Zahra Heydari
 
Dependency injection using Google guice
Dependency injection using Google guiceDependency injection using Google guice
Dependency injection using Google guice
Aman Verma
 
JavaFX in Action Part I
JavaFX in Action Part IJavaFX in Action Part I
JavaFX in Action Part I
Mohammad Hossein Rimaz
 
Design Patterns Part1
Design Patterns  Part1Design Patterns  Part1
Design Patterns Part1
Tom Chen
 
Introduction To Design Patterns
Introduction To Design PatternsIntroduction To Design Patterns
Introduction To Design Patterns
sukumarraju6
 
Benefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design patternBenefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design pattern
Beroza Paul
 
Object Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopObject Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshop
Mohammad Shawahneh
 
Gof design pattern
Gof design patternGof design pattern
Gof design pattern
naveen kumar
 
Mixing Diagram, Tree, Text, Table and Form editors to build a kick-ass modeli...
Mixing Diagram, Tree, Text, Table and Form editors to build a kick-ass modeli...Mixing Diagram, Tree, Text, Table and Form editors to build a kick-ass modeli...
Mixing Diagram, Tree, Text, Table and Form editors to build a kick-ass modeli...
Chauvin Mariot
 
Foster - Getting started with Angular
Foster - Getting started with AngularFoster - Getting started with Angular
Foster - Getting started with Angular
MukundSonaiya1
 
Django Documentation
Django DocumentationDjango Documentation
Django Documentation
Ying wei (Joe) Chou
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
anguraju1
 

Similar to Django Patterns - Pycon India 2014 (20)

Design pattern in android
Design pattern in androidDesign pattern in android
Design pattern in android
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Introduction to django framework
Introduction to django frameworkIntroduction to django framework
Introduction to django framework
 
Design Patterns every Android developer should know
Design Patterns every Android developer should knowDesign Patterns every Android developer should know
Design Patterns every Android developer should know
 
Design patterns in android
Design patterns in androidDesign patterns in android
Design patterns in android
 
Dependency injection using Google guice
Dependency injection using Google guiceDependency injection using Google guice
Dependency injection using Google guice
 
JavaFX in Action Part I
JavaFX in Action Part IJavaFX in Action Part I
JavaFX in Action Part I
 
Design Patterns Part1
Design Patterns  Part1Design Patterns  Part1
Design Patterns Part1
 
Introduction To Design Patterns
Introduction To Design PatternsIntroduction To Design Patterns
Introduction To Design Patterns
 
Benefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design patternBenefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design pattern
 
Object Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopObject Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshop
 
Introduction to django
Introduction to djangoIntroduction to django
Introduction to django
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Gof design pattern
Gof design patternGof design pattern
Gof design pattern
 
Django
DjangoDjango
Django
 
Mixing Diagram, Tree, Text, Table and Form editors to build a kick-ass modeli...
Mixing Diagram, Tree, Text, Table and Form editors to build a kick-ass modeli...Mixing Diagram, Tree, Text, Table and Form editors to build a kick-ass modeli...
Mixing Diagram, Tree, Text, Table and Form editors to build a kick-ass modeli...
 
Foster - Getting started with Angular
Foster - Getting started with AngularFoster - Getting started with Angular
Foster - Getting started with Angular
 
Django Documentation
Django DocumentationDjango Documentation
Django Documentation
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 

Recently uploaded

Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Yara Milbes
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
abdulrafaychaudhry
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 

Recently uploaded (20)

Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 

Django Patterns - Pycon India 2014

  • 1. Django Design Patterns Arun Ravindran
  • 2. About Me Arun Ravindran Blogs and screencasts at arunrocks.com
  • 3. Outline ●Why Design Patterns? ●Does Django follow MVC pattern? ●Model Patterns ●View Patterns ●Template Patterns ●Gyaan on Applying Patterns
  • 4. “ A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design
  • 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. 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. 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. Model Patterns ●Pattern: User Profiles ●Pattern: Service Objects ●Pattern: Custom Model Managers
  • 10. User Profiles (contd.) 1.Define the profile model 2.Listen for User model’s post_save signal
  • 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. User Profiles (contd.) Usage: UserProfile.objects.get_or_create(user=u)
  • 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. Service Objects (contd.) 1.Your model delegates to service objects 2.Services are specialized classes:
  • 16. Custom Model Managers (contd.) 1.Use custom manager for common queries 2.Add it to your model
  • 17. View Patterns ●Pattern: Context Enhancers ●Pattern: Access Controlled Views
  • 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. 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:
  • 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. Template Patterns ●Pattern: Template Inheritance Tree ●Pattern: Active Link
  • 24. Template Inheritance Tree (contd.) 1.Build a base structure, with placeholders 2.Similar pages are extended from base
  • 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. And the list goes on...
  • 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.
  • 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