REST framework: Under the Hood. Saturday, 15 November 14
↕ Serialization Parsing ↑ ↓ Rendering
Problem: Related data.
“Fat models, thin views.” ↓ “Never write to a model field or cal .save() directly. Always use model methods and manager methods for state changing operations.”
Problem: Hidden validation behavior.
We want explicit validation behavior, but we’d stil like the ModelSerializer shortcut.
We can use the ModelSerializer shortcut, but our validation behavior is made visible & explicit. • unique_together • field validation
Consequences of dropping ‘Model.full_clean()’ • All field validation handled. • All uniqueness validation handled. • `Model.clean()` → `Serializer.validate()`
Accessing field information..
Simulating nested and list structures in HTML. .
Avoid coupling fields to HTML too tightly. .
Once we’ve got al that it’s easy enough to write a renderer class that uses templated HTML generation..
The REST framework 3.0 big picture. • Easier to override save behavior. • More explicit validation. • Better model encapsulation. • Less complex internal implementation. • Supports both Form and API output. • Template based form rendering. • Nested forms and lists of forms. • API compatibility with CBVs. • BaseSerializer for custom cases.