Skip to content

Commit

Permalink
Fix Django 4 compatibility issues (#778)
Browse files Browse the repository at this point in the history
* add django 4.0 to tox envs

* make `get_extra_restriction` work across django versions

 - add a shim around `get_extra_restriction` so that it works in
   versions of Django >=4 and <4
 - fixes #776

* update changelog & pypi classifiers

* fix typo in version check
  • Loading branch information
monty5811 committed Dec 16, 2021
1 parent 4042b85 commit d083396
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
~~~~~~~~~~~~

* Add Python 3.10 support.
* Add Django 4.0 support.

2.0.0 (2021-11-14)
~~~~~~~~~~~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ classifiers =
Framework :: Django :: 2.2
Framework :: Django :: 3.1
Framework :: Django :: 3.2
Framework :: Django :: 4.0
Intended Audience :: Developers
License :: OSI Approved :: BSD License
Operating System :: OS Independent
Expand Down
15 changes: 14 additions & 1 deletion taggit/managers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import uuid
from operator import attrgetter

import django
from django.conf import settings
from django.contrib.contenttypes.fields import GenericRelation
from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -666,14 +667,26 @@ def get_joining_columns(self, reverse_join=False):
else:
return (("object_id", self.model._meta.pk.column),)

def get_extra_restriction(self, where_class, alias, related_alias):
def _get_extra_restriction(self, alias, related_alias):
extra_col = self.through._meta.get_field("content_type").column
content_type_ids = [
ContentType.objects.get_for_model(subclass).pk
for subclass in _get_subclasses(self.model)
]
return ExtraJoinRestriction(related_alias, extra_col, content_type_ids)

def _get_extra_restriction_legacy(self, where_class, alias, related_alias):
# this is a shim to maintain compatibility with django < 4.0
return self._get_extra_restriction(alias, related_alias)

# this is required to handle a change in Django 4.0
# https://docs.djangoproject.com/en/4.0/releases/4.0/#miscellaneous
# the signature of the (private) funtion was changed
if django.VERSION < (4, 0):
get_extra_restriction = _get_extra_restriction_legacy
else:
get_extra_restriction = _get_extra_restriction

def get_reverse_joining_columns(self):
return self.get_joining_columns(reverse_join=True)

Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ envlist =
isort
py{36,37,38,39}-dj{22,31}
py{36,37,38,39,310}-dj32
py{38,39,310}-dj40
py{38,39,310}-djmain
docs

Expand All @@ -22,6 +23,7 @@ deps =
dj22: Django>=2.2,<3.0
dj31: Django>=3.1,<3.2
dj32: Django>=3.2,<3.3
dj40: Django>=4.0,<4.1
djmain: https://github.com/django/django/archive/main.tar.gz
coverage
djangorestframework
Expand Down

0 comments on commit d083396

Please sign in to comment.