python metaclass and how django uses them: foss 2010
DESCRIPTION
TRANSCRIPT
![Page 1: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/1.jpg)
Python Meta classes and how Django uses them
Lakshman Prasad
![Page 2: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/2.jpg)
Meta Classes
A Design pattern
Dynamically create classes
Enables Flexibility, Good API
Used by Frameworks
Perform “Magic”
![Page 3: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/3.jpg)
Where Meta Classes?
Frameworks, frameworks, frameworks.
Django uses them extensively.
Scrappy, a web scrapping framework.
Flexible Django apps. Eg: Django-registration
![Page 4: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/4.jpg)
![Page 5: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/5.jpg)
Executes the code when encountered
Creates namespace and stores all variables in it
![Page 6: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/6.jpg)
`type` is an inbuilt MetaClass
Dynamic class = Static classes
![Page 7: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/7.jpg)
`__metaclass__` defines metaclases
![Page 8: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/8.jpg)
Meta Class Magic
Define a class extending object
Examined type is string, Magic!
Enables Declarative Syntax
![Page 9: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/9.jpg)
MetaClasses, not always
Simple function decorators
Unintuitive to end developers, unless explained
Python 2.6+ has Class Decorators
But being powerful, where they are needed, indespesible
![Page 10: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/10.jpg)
Django Models
![Page 11: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/11.jpg)
Django Models Creation
Declarative Syntax, No `__metaclass__`
New model created in place
Options into `Options` object
Register in registry called `AppCache`
Add attributes to new model
![Page 12: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/12.jpg)
Inside Django Models
Return the same registered Model from AppCache
Original options and Meta options stored in `_meta`
Add more useful attributes/classes like `DoesnotExist`, `module_name` and `class_name`
![Page 13: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/13.jpg)
Django Forms
![Page 14: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/14.jpg)
Rendering in html
Validation for right data
Displaying errors
Customization options
Provided model, storing to the database
![Page 15: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/15.jpg)
Dynamic Django forms - 1
Forms API is flexible, but often needs Meta Classes
`base_fields` and `fields` list store all fields
Change field attributes while initialization
![Page 16: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/16.jpg)
Dynamic Django Forms - 2
When high complexity, instantiate a form, dynamically
![Page 17: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/17.jpg)
Resources
Python, Django Documentation, Source.
Pro Django, by Marty Alchin
Django-registration v0.7 by James Bennett
![Page 18: Python Metaclass and How Django uses them: Foss 2010](https://reader033.vdocuments.us/reader033/viewer/2022052522/5463dfa7b4af9f533f8b4750/html5/thumbnails/18.jpg)
Thank You!
: Build Cutting Edge Web Apps.
Small Team of expert Django Developers.
Exclusively develop in Django, Python stack.
@becomingGuru, @uswaretech