Click here to load reader
Date post: | 28-Nov-2014 |
Category: |
Internet |
Upload: | daniel-roseman |
View: | 302 times |
Download: | 6 times |
Click here to load reader
What’s new in Django 1.7Daniel Roseman@danielroseman
blog.roseman.org.ukairpair.me/danielroseman
About me
Web developer previously at Google, Global Radio, Glasses Direct, GDSDjango 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 commandMigrations no longer load initial_dataRunPython - 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 instancesNo need for models.py Hook for “startup” codeAdmin autodiscover called automatically
AppConfigs
Can use path to AppConfig subclass as entry in INSTALLED_APPSOtherwise module is checked for default_app_config variableIf none, uses default classSet app label or verbose_nameready() method for startup code
Checks framework
Static checking for appsLoads of built-in checksCustom 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 querysetPre-filteringCalling 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