Django Patterns - Pycon India 2014

31
Django Design Patterns Arun Ravindran

description

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

Transcript of Django Patterns - Pycon India 2014

Page 1: Django Patterns - Pycon India 2014

Django Design Patterns

Arun Ravindran

Page 2: Django Patterns - Pycon India 2014

About Me

Arun Ravindran Blogs and screencasts at arunrocks.com

Page 3: Django Patterns - Pycon India 2014

Outline

● Why Design Patterns?

● Does Django follow MVC pattern?

● Model Patterns

● View Patterns

● Template Patterns

● Gyaan on Applying Patterns

Page 4: Django Patterns - Pycon India 2014

“ A design pattern is a general reusable

solution to a commonly occurring problem

within a given context in software design

Page 5: Django Patterns - Pycon India 2014

● 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?

Page 6: Django Patterns - Pycon India 2014

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

Page 7: Django Patterns - Pycon India 2014

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

Page 8: Django Patterns - Pycon India 2014

Model Patterns

● Pattern: User Profiles

● Pattern: Service Objects ● Pattern: Custom Model Managers

Page 9: Django Patterns - Pycon India 2014

User Profiles

Page 10: Django Patterns - Pycon India 2014

User Profiles (contd.)

1. Define the profile model

2. Listen for User model’s post_save signal

Page 11: Django Patterns - Pycon India 2014

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

Page 12: Django Patterns - Pycon India 2014

User Profiles (contd.)

Usage: UserProfile.objects.get_or_create(user=u)

Page 13: Django Patterns - Pycon India 2014

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

Page 14: Django Patterns - Pycon India 2014

Service Objects (contd.)

1. Your model delegates to service objects

2. Services are specialized classes:

Page 15: Django Patterns - Pycon India 2014

Custom Model Managers

Page 16: Django Patterns - Pycon India 2014

Custom Model Managers (contd.)

1. Use custom manager for common queries

2. Add it to your model

Page 17: Django Patterns - Pycon India 2014

View Patterns

● Pattern: Context Enhancers ● Pattern: Access Controlled Views

Page 18: Django Patterns - Pycon India 2014

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

Page 19: Django Patterns - Pycon India 2014

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:

Page 20: Django Patterns - Pycon India 2014

Access Controlled Views

Page 21: Django Patterns - Pycon India 2014

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

Page 22: Django Patterns - Pycon India 2014

Template Patterns

● Pattern: Template Inheritance Tree ● Pattern: Active Link

Page 23: Django Patterns - Pycon India 2014

Template Inheritance Tree

Page 24: Django Patterns - Pycon India 2014

Template Inheritance Tree (contd.)

1. Build a base structure, with placeholders

2. Similar pages are extended from base

Page 25: Django Patterns - Pycon India 2014

Active Link

Page 26: Django Patterns - Pycon India 2014

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

Page 27: Django Patterns - Pycon India 2014

And the list goes on...

Page 28: Django Patterns - Pycon India 2014

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.

Page 29: Django Patterns - Pycon India 2014

PATTERNS ARE EVERYWHERE

Page 30: Django Patterns - Pycon India 2014

Thank you!

Page 31: Django Patterns - Pycon India 2014

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