Django Patterns - Pycon India 2014
description
Transcript of Django Patterns - Pycon India 2014
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:
o Don’t blindly apply. Explain why.
o Don’t apply a pattern if you language supports a
direct solution
o Don’t try to retro-fit everything in terms of patterns
o Don’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