Upgrading from Django 3.0.9 to 3.1 was a breeze.
To be fair upgrading Django is usually a breeze. The exhausive release notes written every time a new version is rolled out is a defining factor here.
What do I mean when I say “a breeze”? I compare Django “upgrades” to something like upgrading a Python 2 codebase to Python 3 😬
So, upgrades being a breeze, why write a post about this topic?
Because a Django application usually does not comprise only of the Django package.
How I upgraded
This section basically is about “how I usually upgrade”.
Made specific for this
requirements.txtfile. I.e. changed entry from
pip install -r requirements.txtwith the updated
requirementsfile. Among the command’s output I spotted:
Installing collected packages: Django Found existing installation: Django 3.0.9 Uninstalling Django-3.0.9: Successfully uninstalled Django-3.0.9 Successfully installed Django-3.1
This yielded several warnings of the type:
$ ./manage.py check System check identified some issues: WARNINGS: app_name.Model.field_name: (fields.W904) django.contrib.postgres.fields.JSONField is deprecated. Support for it (except in historical migrations) will be removed in Django 4.0. HINT: Use django.db.models.JSONField instead. ... System check identified 3 issues (0 silenced).
Hence I search/replaced any instances of
from django.contrib.postgres.fields import JSONField
from django.db.models import JSONField
./manage.py checkagain. Got this output:
System check identified no issues (0 silenced)
- In order to keep the Django models code and database state in sync, I ran
./manage.py makemigrations appname -n move_to_django_jsonfield
Done this for each app containing a
This resulted in
AlterField statements for the models/fields whose
JSONField type was updated:
migrations.AlterField( model_name='mymodel', name='myfield', field=models.JSONField(), ),
- Ran tests.
Oh noes! An external Python packages I use only when running tests,
django_bakery, does not like Django
3.1. I got this exception:
TypeError: <class 'django.db.models.fields.json.JSONField'> is not supported by baker.
Non-Django issue and what I did about it
The Django application itself works without error. Unit tests that rely on the Django 3.1
JSONField fail. Because the model_bakery package does not support the newly added
First thought: Am I running the latest package?
The below command didn’t install a new version.
pip install model_bakery -U
So I have the latest.
Second thought: So is this an opportunity for me to write a PR? Finally make a decent contribution to a package I really like? To an opensource project which has given me so much?
Not so fast 😊
I navigated to the pacakge’s github repo and looked for the text
JSONField within the closed PRs. And indeed:
So the question now is: How do I get to use the package with this commit in my project?
Pinning a github project to a specific commit
This StackOverflow answer presents various ways of achieving this.
model_bakery requirements file entry I had looked like:
whereas now I have:
What I did is not best practice. Far from it. But I only use
model_bakery package when running unit tests. I’m OK with taking that sort of risk with a package that does not “run” in production. I’m willing to accept that risk.
I find Django a very stable framework. But no open source project can guarantee “zero hiccups” for the whole ecosystem around it.
The approach I’ve taken above is a compromise between:
- the stability of a mature framework like Django, and
- utilising the rich ecosystem around it
Any thoughts? Should I have done it another way? Did I ignore any risks?