Pinax 18.01 Upgrading app for Django v2.0
KatherineMichel edited this page Jan 12, 2020
·
3 revisions
Bring a Pinax app into compliance with Django v2.0 by following these general steps:
- Use an updated
tox.ini
file, as seen in Converting to CircleCi. This update updates the test matrix, removing deprecated versions of Django. - Run
detox
locally to see which test configurations fail and why.
- Add
on_delete=models.CASCADE
(or some other value) to allForeignKey
andOneToOne
model fields. - Use (or create) compat.py for import compatibility. Currently the only import issue we know is
mock
which did not exist inunittest
until Python 3.3. See pinax-announcements compat.py for example imports, then usefrom .compat import mock
for example. - Add URL namespacing. Add
app_name = "pinax_announcements"
or similar to urls.py. - Import from
django.urls
instead ofdjango.core.urlresolvers
. - Change
MIDDLEWARE_CLASSES
toMIDDLEWARE
as per https://docs.djangoproject.com/en/1.11/topics/http/middleware/. - Use
django.shortcuts.render()
in place of deprecateddjango.shortcuts.render_to_response()
. - Use
User.is_authenticated
andUser.is_anonymous
properties in place of deprecatedUser.is_authenticated()
andUser.is_anonymous()
functions. - Remove references to removed
SessionAuthenticationMiddleware
class. It provided no functionality since session authentication is unconditionally enabled in Django 1.10. - Change
@register.assignment_tag
to@register.simple_tag
. See Simple Tag guidance. - Update documentation for
INSTALLED_APPS
value as a list instead of a tuple, i.e.INSTALLED_APPS = [ "app", ]
. Check for other settings with the same issue. - Search for uses of django.contrib.auth
User
model. Consider usingfrom django.contrib.auth import get_user_model
instead. - If your application URLs includes
url(r"^admin/", include(admin.site.urls))
, change that tourl(r"^admin/", admin.site.urls)
. - Replace
context_instance=RequestContext(self.request)
kwarg torender_*()
functions withrequest=self.request
.
As usual before a new release, update the version number. Recently the Pinax team decided this update, with corresponding drop of older Django version support, warrants a major version upgrade, i.e. from v2.0.4 to v3.0.0.
See pinax-announcements files for an example of these guidelines implemented in the real world.