Upgrading to Django 1.3

Get Started. It's Free
or sign up with your email address
Upgrading to Django 1.3 by Mind Map: Upgrading to Django 1.3

1. Procedure

1.1. Use VirtualEnv + pip

1.1.1. VirtualEnv install activate

1.1.2. pip freeze deps

1.1.3. Strongly recommend to learn them

1.1.4. Douglas: Once you master VirtualEnv, also try VirtualEnvWrapper

1.2. Upgrade just Django

1.2.1. pip install --upgrade Django

1.2.2. ./manage.py test

1.2.3. Things to watch for (in general) Accessing internals such as always document doing that have tests for that code Arbitrary arguments such as remember **kwargs Monkeypatches Prone to break Deprecated APIs & backward incompatible changes Check not getting warnings

1.3. Test

1.4. Upgrade 3rd party apps

1.4.1. Most already support 1.3

1.4.2. Upgrade them 1 by 1

1.4.3. Use pip Declaratively configure dependencies & versions

1.5. Test

1.6. Deploy

1.6.1. command line pip freeze > requirements.txt pip install -r requirements.txt

1.6.2. to deal with inconsistency during upgrade shutdown apache if you have several servers, upgrade them 1 by 1

2. Deprecated & removed

2.1. AdminSite.root()

2.1.1. (r'^admin/', include(admin.site.urls)), part of a large refactoring to make the admin more customizable

2.2. Auth backends that don't support anonymous

2.2.1. now they must class MyAuthBackend(object): support_object_permissions = False support_anonymous_user = False support_inactive_user = False def authenticate(self, *kw): ...

2.3. more here:

2.3.1. http://django.me/deprecation

3. Backwards-incompatible changes

3.1. Usually due to

3.1.1. security fixes

3.1.2. data-loss bugs

3.2. Security fixes

3.2.1. Added CSRF validation for AJAX requests prior to 1.3 they were exempted from CSRF validations because it wa assumed that browsers won't allow cross-site scripting but a Flash vulnerabilyty does expose this risk $.ajaxSetup({ beforeSend: function(xhr, settings) { http://django.me/csrf-ajax Placed restrictions on filters in the admin Stopped rendering passwords in PasswordInput Stopped allowing password resets for inactive users

3.3. Data-loss bug: FileField file deleteion

3.3.1. https://gist.github.com/889692 but be careful when cleaning orphan files

3.4. Optimizations

3.4.1. Manually-managed transactions need to be expllicitly closed @transaction.commit_manually

3.4.2. New index on session table python manage.py sqlindexes sessions will make it faster Anyway recommends not using db sessions either memcached, or redis

3.5. The rest

3.5.1. Clearable FileField widget is default

3.5.2. No more PROFANITIES_LIST in comments never really worked in the 1st place

3.5.3. Localflavor corrections for Canada, Indonesia & the USA

3.5.4. FormSets can no longer take empty data

3.5.5. Initial SQL files no longer work in tests use fixtures instead


5. better tear-down

6. Better time-zone support

7. The new hotness


7.1.1. Improved template tags {% with total=managers.count name=... %} {% include "template.html" with total=... %} {% load my_tag from my_tag_lib %} {% load url from future %} {% url "path.to.view" %} {% url my_var arg kw=arg2 %}

7.1.2. Class-based generic views Template view class HelpView(TemplateView): template_name = "..." url('..', HelpView.as_view()) Model views DetailView Base View class All generic views You need to extend them Implementation Each comprised of mix-ins You can construct your own mix-ins Replacing some mix-ins with our own See example in docs of JSONDetailView Read about Python's MRO Should you use CBV's? Wait till there's more experience with it

7.2. Model "on delete" options

7.2.1. (Jacob's favorite new feature)

7.2.2. in 1.2 deletes cascade delete author will delete its books

7.2.3. author = models.ForeignKey(Person, on_delete=models.PROTECT) will throw error when deleting author with books

7.2.4. on_delete=models.DO_NOTHING leave it to the DB handle it E.g., MySQL will throw integrity error

7.2.5. on_delete=models.SET_NULL books Author will be set to null

7.2.6. on_delete=models.SET(get_dummy_person) callback that will be called to set the author field of books e.g., creates a person called: DELETED

7.2.7. on_delete=<your own callback> def CASCADE_AND_PRINT(collector, field, sub_objs, using): print "Deleting", sub_objs return models.CASCADE(collector, field, sub_objs, using)

7.3. Testing

7.3.1. unittest2 will be part of 2.7 bundled in 1.3 vastly improved failure messages shows smart diff of skipping tests @unittest.skipIf(platform.system() != 'Linux', "only works on linux") skip skipIf skipUnless(condition, 'skipped if not condition') skipIfDBFeature('supports_transactions') skipUnlessDBFeature assertRaises context manager class & module level setUp/tearDown usage from django.test import TestCase class MyTest(TestCase): http://django.me/testing

7.3.2. RequestFactory in 1.2 r = self.client.get('/some/view') self.assertEqual(r.status_code, 200) in 1,3 rf = django.test.client.RequestFactory() request = rf.get('/some/view') response = some_view(request) self.assertEqual(response.status_code, 200) directly invokes view, without the full stack middleware &c Jacob says he'll now write view tests like this

7.3.3. assertNumQueries with self.assertNumQueries(2): self.client.get('/some/view/')

7.4. Caching

7.4.1. Multiple caches settings.CACHES like databases old syntax still work, but at some point you'll need to upgrade syntax CACHES = { c1 = cache.get_cache('default') c1.set(...) changing version will invalidate all previous cache there's a python API to change the version

7.4.2. versioning

7.4.3. site-wide prefixing

7.4.4. transformation

7.4.5. pylibmc support new & much faster memcached

7.4.6. Suggestions: switch to pylibmc enable versioning

7.5. Static files

7.5.1. clears confusion

7.5.2. Definitions "media" files uploaded by users "static" static assets that are part of your site "files" either of the above

7.5.3. pluggable apps have their own static files

7.5.4. new contrib app django.contrib.staticfiles collects static files from multiple apps into a central location & just that

7.5.5. usage in developement add to INSTALLED_APPS put static files in set STATIC_URL to "/static/" in production use dedicated media server STATIC_ROOT = '/var/www/static/' STATIC_URL = 'http://media.example.com/' ./manage.py collectstatic

7.5.6. in templates {% load static %} ... see docs

7.5.7. Suggestion Switch if your current scheme makes you unhappy

7.5.8. There are more 3rd party apps with more features compression sprites caching &c see in djangopackages.com djangopackages.com > Grids > G > Asset-managers django-storages use S3 &c

7.6. Everything else

7.6.1. Logging built-in support for Python logging http://django.me/logging Read docs - really good intro

7.6.2. TemplateResponse similar to HttpResponse, yet Lazy easier to do manipulations before rendering http://django.me/TemplateResponse

7.6.3. django.shortcuts.render() can use the requst processor use it

7.6.4. Transactions as context managers

7.6.5. "Pretty" error emails like the debug pages

7.6.6. "Current site" helper get_current_site

7.6.7. HTTPOnly cookies http://django.me/set_cookie

7.6.8. Context-aware simple tags

8. What's coming next

8.1. Planning for deprecation

8.1.1. to be removed in 1.4 Python 2.4 support single-db support User-based messages user.message_set.create() Legacy auth backends supports_anonymous_user &c must be true & more

8.1.2. to be removed in 1.5 Python 2.5 support (?) 2.6 much faster mod_python support you can't event download it anymore use mod_wsgi Function based generic views Old style url & ssi template tags

8.2. New features (speculations)

8.2.1. Basic schema migration support bringing basic low level south api's

8.2.2. Refactor & formalization of "apps"

8.2.3. Improved ideas of what a "user" is

8.2.4. Better hooks for monitoring, debugging, metrics

8.2.5. Template compilation speed improvements

8.2.6. Python 3 support experimental probably