Slide 1

Slide 1 text

Under the hood Marty Alchin http://martyalchin.com/ PyCon 2008, Chicago

Slide 2

Slide 2 text

So you know Django Declarative syntax for models and forms Easy-to-use ORM Extensible template language Automatic admin interface It slices! It dices! It makes Julienne fries!

Slide 3

Slide 3 text

But wait! There’s more!

Slide 4

Slide 4 text

Pluggable features HTTP middleware Database connections Cache mechanisms Authentication providers Context processors Template loaders Management commands

Slide 5

Slide 5 text

And coming soon to SVN near you...

Slide 6

Slide 6 text

Upcoming features Easy admin customizations Flexible QuerySet subclassing File storage options No doubt more to follow

Slide 7

Slide 7 text

How do they do it?

Slide 8

Slide 8 text

Tools of the trade Metaclasses Signals using PyDispatcher Several utility functions A ton of other stuff beyond today’s talk

Slide 9

Slide 9 text

Metaclasses

Slide 10

Slide 10 text

[Metaclasses] are deeper magic than 99% of users should ever worry about. If you wonder whether you need them, you don’t (the people who actually need them know with certainty that they need them, and don’t need an explanation about why). Tim Peters, comp.lang.python, December 22, 2002 “ “ “

Slide 11

Slide 11 text

Welcome to the 1%

Slide 12

Slide 12 text

Uses of metaclasses Tracking subclasses Discovering inheritance schemes Providing custom methods Inspecting class declarations Plenty more

Slide 13

Slide 13 text

Uses within Django Caching a list of known models Moving Meta options into the special _meta class attribute Moving model fields out to an internal list Keeping track of model inheritance Collecting form fields into an internal list Merging fields from inherited forms

Slide 14

Slide 14 text

Model Model metaclasses Model SlugField ForeignKey CharField IntegerFIeld __unicode__() get_absolute_url() Meta verbose_name verbose_name_plural ordering order_with_respect_to Model __unicode__() get_absolute_url() _meta verbose_name verbose_name_plural ordering order_with_respect_to fields SlugField ForeignKey CharField IntegerFIeld

Slide 15

Slide 15 text

contribute_to_class(cls, name)

Slide 16

Slide 16 text

Signals

Slide 17

Slide 17 text

How signals work post_init listener listener listener listener() listener() listener() Model()

Slide 18

Slide 18 text

Signals in the box pre_init post_init pre_save post_save pre_delete post_delete class_prepared post_syncdb request_started request_finished got_request_exception template_rendered

Slide 19

Slide 19 text

And you can too! my_signal = object() Document its location Document when it fires Document what parameters are sent along

Slide 20

Slide 20 text

Utility functions

Slide 21

Slide 21 text

Functional utilities Curried functions preload arguments Wrapped functions preserve attributes Memoized functions improve performance Lazy functions evaluate when accessed Turn middleware into per-view decorators Standard decorators are fair game Don’t forget inspect

Slide 22

Slide 22 text

Text utilities Capitalize the first letter of a string Split text, preserving quoted passages Truncate text, preserving words or HTML Display a list of values in natural English Convert text to its phone number equivalent Powerful template engine is available Standard string formatting still works

Slide 23

Slide 23 text

Just remember...

Slide 24

Slide 24 text

Django is Python

Slide 25

Slide 25 text

Questions? You know you want to