Page MenuHomePhabricator

Upgrade from django2.2 to django 3.2
Closed, ResolvedPublic

Description

Django 2.2 is officially going out of LTS by April. We need to start thinking about how upgrade from django2.2 to django3.2

Event Timeline

bd808 triaged this task as Medium priority.Apr 4 2022, 9:14 PM
bd808 moved this task from Backlog to Groomed/Ready on the Toolhub board.

How do I enable Django deprecation warnings?

Slst2020 added a project: User-Slst2020.
Slst2020 moved this task from To Do to Doing on the User-Slst2020 board.

How do I enable Django deprecation warnings?

I think with either a PYTHONWARNINGS="d" environment variable or python3 -Wd ... when running a script.

I haven't tried this myself yet, but I think you could set the PYTHONWARNINGS envvar in the docker-compose.yaml file to try things out.

Upgrading from 2.2 → 3.0 → 3.1 seems to go well, but 3.1 → 3.2 is breaking:

Exception in thread django-main-thread:

Traceback (most recent call last):

  File "/opt/lib/poetry/toolhub-2uZo5AhP-py3.7/lib/python3.7/site-packages/django/apps/config.py", line 244, in create

    app_module = import_module(app_name)

  File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import

  File "<frozen importlib._bootstrap>", line 983, in _find_and_load

  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked

ModuleNotFoundError: No module named 'auditlog'


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner

    self.run()

  File "/usr/lib/python3.7/threading.py", line 865, in run

    self._target(*self._args, **self._kwargs)

  File "/opt/lib/poetry/toolhub-2uZo5AhP-py3.7/lib/python3.7/site-packages/django/utils/autoreload.py", line 64, in wrapper

    fn(*args, **kwargs)

  File "/opt/lib/poetry/toolhub-2uZo5AhP-py3.7/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run

    autoreload.raise_last_exception()

  File "/opt/lib/poetry/toolhub-2uZo5AhP-py3.7/lib/python3.7/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception

    raise _exception[1]

  File "/opt/lib/poetry/toolhub-2uZo5AhP-py3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute

    autoreload.check_errors(django.setup)()

  File "/opt/lib/poetry/toolhub-2uZo5AhP-py3.7/lib/python3.7/site-packages/django/utils/autoreload.py", line 64, in wrapper

    fn(*args, **kwargs)

  File "/opt/lib/poetry/toolhub-2uZo5AhP-py3.7/lib/python3.7/site-packages/django/__init__.py", line 24, in setup

    apps.populate(settings.INSTALLED_APPS)

  File "/opt/lib/poetry/toolhub-2uZo5AhP-py3.7/lib/python3.7/site-packages/django/apps/registry.py", line 91, in populate

    app_config = AppConfig.create(entry)

  File "/opt/lib/poetry/toolhub-2uZo5AhP-py3.7/lib/python3.7/site-packages/django/apps/config.py", line 250, in create

    app_config_class.__qualname__,

django.core.exceptions.ImproperlyConfigured: Cannot import 'auditlog'. Check that 'toolhub.apps.auditlog.apps.AuditlogConfig.name' is correct.

Change 778221 had a related patch set uploaded (by Slavina Stefanova; author: Slavina Stefanova):

[wikimedia/toolhub@main] dev: Upgrade Django from version 2.2 to 3.2

https://gerrit.wikimedia.org/r/778221

Updating from 2.2 -> 3.0 -> 3.1 did not break anything.

3.1 -> 3.2 had only one breaking change:
https://docs.djangoproject.com/en/3.2/releases/3.2/#automatic-appconfig-discovery
https://docs.djangoproject.com/en/3.2/ref/applications/#django.apps.AppConfig.name

However, running the unit tests generated a bunch of warnings related to auto-created primary keys:
https://docs.djangoproject.com/en/3.2/releases/3.2/#customizing-type-of-auto-created-primary-keys

When defining a model, if no field in a model is defined with primary_key=True an implicit primary key is added. The type of this implicit primary key can now be controlled via the DEFAULT_AUTO_FIELD setting and AppConfig.default_auto_field attribute. No more needing to override primary keys in all models.

Maintaining the historical behavior, the default value for DEFAULT_AUTO_FIELD is AutoField. Starting with 3.2 new projects are generated with DEFAULT_AUTO_FIELD set to BigAutoField. Also, new apps are generated with AppConfig.default_auto_field set to BigAutoField. In a future Django release the default value of DEFAULT_AUTO_FIELD will be changed to BigAutoField.

To avoid unwanted migrations in the future, either explicitly set DEFAULT_AUTO_FIELD to AutoField.

I have set DEFAULT_AUTO_FIELD to AutoField globally inside settings.py; this could also be configured on a per-app or per-model basis.

Change 778221 merged by jenkins-bot:

[wikimedia/toolhub@main] dev: Upgrade Django from version 2.2 to 3.2

https://gerrit.wikimedia.org/r/778221

Change 786342 had a related patch set uploaded (by BryanDavis; author: Bryan Davis):

[operations/deployment-charts@master] toolhub: Bump container version to 2022-04-21-215651-production

https://gerrit.wikimedia.org/r/786342

Change 786342 merged by jenkins-bot:

[operations/deployment-charts@master] toolhub: Bump container version to 2022-04-21-215651-production

https://gerrit.wikimedia.org/r/786342