Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Django: Under the Hood

Django: Under the Hood

Presented at PyCon 2008. Dive into the mechanisms Django uses to achieve some of its best-known features and you can take advantage of that knowledge to do some pretty amazing things.

Ginger Hawthorne

September 27, 2011
Tweet

Other Decks in Programming

Transcript

  1. 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!
  2. Tools of the trade Metaclasses Signals using PyDispatcher Several utility

    functions A ton of other stuff beyond today’s talk
  3. [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 “ “ “
  4. 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
  5. 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
  6. 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
  7. And you can too! my_signal = object() Document its location

    Document when it fires Document what parameters are sent along
  8. 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
  9. 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