Xiangxiang's Personal Site

Machine Learning & Security Engineer
生命不息,折腾不止,留下一点活着的记录.

View on GitHub
30 December 2016

Django in depth

by xiangxiang

学习Django in Depth - James Bennett - PyCon 2015 ppt

Part 1: ORM

Model
  |
Manager
  |
QuerySet
  |
Query
  |
SQLComplier
  |
Database backend 

Model

Manager

QuerySet (django/db/models/query.py)

Query (django/db/models/sql/query.py)

In simple terms, a Query is a tree-like data structure, and turning it into SQL involves going through its various nodes, calling the as_sql() method of each one to get its output.

The bridge (multi database backend support)

  1. Query.get_compiler() returns a SQLCompiler instance for that Query
  2. Which calls the compiler() method of DatabaseOperations, passing the name of the desired compiler
  3. Which in turn looks at DatabaseOperations.compiler_module to find location of SQLCompiler classes for current backend

SQLComplier

Database backend

Flavors of inheritance

  1. Abstract parents
    • abstract = True in the Meta declaration
    • Subclasses, if not abstract, generate a table with both their own fields and the abstract parent’s
    • Subclasses can subclass and/or override abstract parent’s Meta
  2. Multi-table
    • No special syntax: just subclass the other model
    • Can’t directly subclass parent Meta, but can override by re-declaring options
    • Can add new fields, managers, etc.
    • Subclass has implicit OneToOneField to parent
  3. Proxy models
    • Subclass parent, declare proxy = True in Meta
    • Will reuse parent’s table and fields; only allowed changes are at the Python level
    • Proxy subclass can define additional methods, manager, etc
    • Queries return instances of the model queried; query a proxy, get instances of the proxy

Unmanaged models

Unmanaged models are mostly useful for wrapping a pre-existing DB table, or DB view,
that you don’t want Django to try to mess with.
While they can be used to emulate some aspects of model inheritance, 
declaring a proxy subclass of a model is almost always a better way of doing that.

Part 2: Forms

Widgets

Fields

FIELDS AND WIDGETS

Forms (Tie fields/widgets all together in a neat package)

ModelForm: Model <-> form conversion

Media support (form media)

Actually, any class can support media definitions; 
widgets and forms are just the ones that support them by default.
To add media to another class, have that class use the metaclass django.forms.widgets.MediaDefiningClass. 
It will parse an inner Media declaration and turn it into a media property with the same behaviors
(i.e., combinable, prints as HTML, etc.) as on forms.

Part 3: Template

Django templates

Django ships built-in backends for its own template system, creatively called the Django template language (DTL), and for the popular alternative Jinja2

Engine

Loader

Template objects

Tag and filter libraries

Context

The Django template language

Part 4: Request/Response processing

Entry point

Handler lifecycle

tags: python django