pipenvの仮想環境のDjango3.0をDjango3.2にアップデートする

この記事ではpipenvで作った仮想環境のDjango3.0.2をDjango3.2にアップデートする方法をご紹介します。

現時点のDjangoの最新バージョンは4.0ですが、LTS(長期サポート)ではないためDjango3.2にアップデートします。
Django3.2は2024年4月までサポートされます。

Supported Versionsでバージョンごとのサポート期間をみることができますので参照してください。

https://www.djangoproject.com/download/

目次

Django3.2をインストール

まずはpipenvを使ってDjangoをインストールしようとしましたが、pillowがインストールできなかったのでDjangoもインストールできませんでした。

$ pipenv install Django
Installing Django…
Adding Django to Pipfile's [packages]…
✔ Installation Succeeded 
Installing dependencies from Pipfile.lock (8f0731)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3/3 — 00:00:00
An error occurred while installing pillow==7.0.0 --hash=sha256:c5ed816632204a2fc9486d784d8e0d0ae754347aba99c811458d69fcdfd2a2f9
...

/T/pip-record-1og3j8a3/install-record.txt --single-version-externally-managed --compile --install-headers /Users/horihideyuki/source/manage/v3/.venv/include/site/python3.8/pillow Check the logs for full command output.
ERROR: Couldn't install package: pillow
 Package installation failed...
  ☤  ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/1 — 00:00

一旦すべて削除してDjango3.2を入れ直します。

pipenvで作った仮想環境に関するファイルとパッケージを出力しているrequirement.txtをすべて削除します。

$ rm -rf .venv Pipfile .Pipfile.lock requirement.txt

改めてpipenvで仮想環境を作って仮想環境に入ります。

$ pipenv --python 3.8
$ pipenv shell

Django3.2のバージョンを指定してインストール。
今度はDjangoをインストールできました。

$ pipenv install Django==3.2

Django3.2のインストールが確認できたので他のパッケージもインストールします。

$ pipenv install djangorestframework
$ pipenv install --dev django-debug-toolbar django-webpack-loader

pipenvでインストールしたパッケージをrequirements.txtに出力しておきます。

$ pipenv lock -r > requirements.txt

テストを実行して非推奨となった機能を修正する

python -Wa manage.py testでテストを実行して3.2で非推奨になった機能などがないか確認します。
もし非推奨になった機能などがあれば警告を出してくれます。

$ python -Wa manage.py test

/Users/horihideyuki/source/manage/v2/apps/accounts/models.py:56: RemovedInDjango40Warning: django.utils.translation.ugettext_lazy() is deprecated in favor of django.utils.translation.gettext_lazy().
  email = models.EmailField(_('email address'), unique=True)
/Users/horihideyuki/source/manage/v2/apps/accounts/models.py:57: RemovedInDjango40Warning: django.utils.translation.ugettext_lazy() is deprecated in favor of django.utils.translation.gettext_lazy().
  first_name = models.CharField(_('first name'), max_length=30, blank=True)
/Users/horihideyuki/source/manage/v2/apps/accounts/models.py:58: RemovedInDjango40Warning: django.utils.translation.ugettext_lazy() is deprecated in favor of django.utils.translation.gettext_lazy().
  last_name = models.CharField(_('last name'), max_length=30, blank=True)
...

たくさん非推奨となった機能があったのでこれらを修正します。

修正できたらまたpython -Wa manage.py testを事項して警告が出ないか確認します。
まだ警告が出ましたので対応します。

$ python -Wa manage.py test
...
django.core.exceptions.ImproperlyConfigured: Cannot import 'accounts'. Check that 'apps.stores.apps.AccountsConfig.name' is correct.

Django3.2ではAppConfigのnameにプロジェクトのルートディレクトリからアプリケーションまでの名前空間を指定する必要があります。
そのため以下のように名前空間を指定して修正します。

from django.apps import AppConfig


class AccountsConfig(AppConfig):
    # name = 'accounts'
    name = 'apps.accounts'

もう一度テストを実行すると警告がすべて消えました。

$ python -Wa manage.py test
...

System check identified 49 issues (0 silenced).

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

Django3.2を立ち上げる

次にDjangoを立ち上げてみます。
Djangoは立ち上がりましたが、ここでも警告が出たので修正していきます。

$ pipenv run server
Loading .env environment variables…
Watching for file changes with StatReloader
Performing system checks...

System check identified some issues:

WARNINGS:
accounts.User: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
	HINT: Configure the DEFAULT_AUTO_FIELD setting or the AccountsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
...


System check identified 49 issues (0 silenced).
March 25, 2022 - 14:26:47
Django version 3.2, using settings 'config.settings.development'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

この警告はモデルに主キーが設定されていないために出る警告です。
Django3.2で変更された「Customizing type of auto-created primary keys」によるものです。

settings.pyにDEFAULT_AUTO_FIELDを次のように設定するか、

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

または

class User(models.Model):
    id = models.AutoField(primary_key=True)
    ...

の様に設定する必要があります。

修正後にDjangoを立ち上げ直してみると、今度は警告がでませんでした。
各機能も問題なく動作します。

$ pipenv run server
Loading .env environment variables…
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
March 25, 2022 - 14:28:21
Django version 3.2, using settings 'config.settings.development'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

コメント

コメントする


The reCAPTCHA verification period has expired. Please reload the page.

目次