KEMBAR78
What's new in Django 1.7 | PDF
What’s new in Django 1.7 
Daniel Roseman 
@danielroseman 
blog.roseman.org.uk 
airpair.me/danielroseman
About me 
Web developer previously at Google, Global 
Radio, Glasses Direct, GDS 
Django user since version 0.95 
Occasional blogger on Django, Python, vim 
Top answerer for Django at StackOverflow
What is new 
● Migrations 
● App loading 
● System checks 
● Managers from custom QuerySets 
● Prefetch object 
● Custom lookups and transforms 
● Form errors improvements
Migrations 
Awesome! 
But… functionality is basically the same as 
South
Automatic migrations 
● Detects schema changes 
● Add/remove columns, indexes, tables 
● Dependencies 
manage.py makemigrations <app> 
manage.py migrate
Migrations 
No separate datamigration command 
Migrations no longer load initial_data 
RunPython - arbitrary functionality: 
callable(apps, schema_editor) 
RunSQL replaces deprecated initial SQL data 
loading
App registry 
List of registered apps: 
from django.apps import apps 
Consists of AppConfig instances 
No need for models.py 
Hook for “startup” code 
Admin autodiscover called automatically
AppConfigs 
Can use path to AppConfig subclass as entry in 
INSTALLED_APPS 
Otherwise module is checked for 
default_app_config variable 
If none, uses default class 
Set app label or verbose_name 
ready() method for startup code
Checks framework 
Static checking for apps 
Loads of built-in checks 
Custom checks - eg on field subclasses, or 
base model classes 
./manage.py check
Custom QuerySets 
Previously, create a custom Manager with extra 
methods - but it returns a plain QuerySet 
Instead, create custom QuerySet, and use 
as_manager() to create manager 
automatically
Custom manager on relations 
Choose which manager is used to return 
related queryset 
Pre-filtering 
Calling custom methods 
my_instance.related_set( 
manager='my_manager')
Prefetch object 
Control operation of prefetch_related() 
● Specify base QuerySet for lookup - eg to 
filter relation, or call select_related() 
● to_attr to save result to custom attribute 
● can call multiple times with different 
querysets
Lookups and Transforms 
Custom lookups: 
MyModel.objects.filter(foo__ne='bar') 
Transform comparisons: 
MyModel.objects.filter(foo__lq='bar')
Form errors 
Simpler way to add errors from clean() method 
or from the view: form.add_error() 
Get raw error objects: 
form.errors.as_data() 
Serialize errors to JSON: 
form.errors.as_json() 
NON_FIELD_ERROR key in Meta.error_messages
Miscellaneous 
● Field subclass deconstruct() method 
● QuerySet .update_or_create() 
● JsonResponse class 
● Template {% include %} can use anything 
with a render method 
● Lots more admin control - site_header, 
site_title, index_title, get_fields(), 
get_search_fields(), view_on_site…
The future 
GSoC student Daniel Pyrathon: refactoring 
Meta to stable API 
Allows any arbitrary class to define Meta and 
therefore make use of admin, model forms, etc 
Example: GMail store
What's new in Django 1.7

What's new in Django 1.7

  • 1.
    What’s new inDjango 1.7 Daniel Roseman @danielroseman blog.roseman.org.uk airpair.me/danielroseman
  • 2.
    About me Webdeveloper previously at Google, Global Radio, Glasses Direct, GDS Django user since version 0.95 Occasional blogger on Django, Python, vim Top answerer for Django at StackOverflow
  • 3.
    What is new ● Migrations ● App loading ● System checks ● Managers from custom QuerySets ● Prefetch object ● Custom lookups and transforms ● Form errors improvements
  • 4.
    Migrations Awesome! But…functionality is basically the same as South
  • 5.
    Automatic migrations ●Detects schema changes ● Add/remove columns, indexes, tables ● Dependencies manage.py makemigrations <app> manage.py migrate
  • 6.
    Migrations No separatedatamigration command Migrations no longer load initial_data RunPython - arbitrary functionality: callable(apps, schema_editor) RunSQL replaces deprecated initial SQL data loading
  • 7.
    App registry Listof registered apps: from django.apps import apps Consists of AppConfig instances No need for models.py Hook for “startup” code Admin autodiscover called automatically
  • 8.
    AppConfigs Can usepath to AppConfig subclass as entry in INSTALLED_APPS Otherwise module is checked for default_app_config variable If none, uses default class Set app label or verbose_name ready() method for startup code
  • 9.
    Checks framework Staticchecking for apps Loads of built-in checks Custom checks - eg on field subclasses, or base model classes ./manage.py check
  • 10.
    Custom QuerySets Previously,create a custom Manager with extra methods - but it returns a plain QuerySet Instead, create custom QuerySet, and use as_manager() to create manager automatically
  • 11.
    Custom manager onrelations Choose which manager is used to return related queryset Pre-filtering Calling custom methods my_instance.related_set( manager='my_manager')
  • 12.
    Prefetch object Controloperation of prefetch_related() ● Specify base QuerySet for lookup - eg to filter relation, or call select_related() ● to_attr to save result to custom attribute ● can call multiple times with different querysets
  • 13.
    Lookups and Transforms Custom lookups: MyModel.objects.filter(foo__ne='bar') Transform comparisons: MyModel.objects.filter(foo__lq='bar')
  • 14.
    Form errors Simplerway to add errors from clean() method or from the view: form.add_error() Get raw error objects: form.errors.as_data() Serialize errors to JSON: form.errors.as_json() NON_FIELD_ERROR key in Meta.error_messages
  • 15.
    Miscellaneous ● Fieldsubclass deconstruct() method ● QuerySet .update_or_create() ● JsonResponse class ● Template {% include %} can use anything with a render method ● Lots more admin control - site_header, site_title, index_title, get_fields(), get_search_fields(), view_on_site…
  • 16.
    The future GSoCstudent Daniel Pyrathon: refactoring Meta to stable API Allows any arbitrary class to define Meta and therefore make use of admin, model forms, etc Example: GMail store