Skip to content

Commit c005410

Browse files
authored
Merge pull request #461 from AdamDonna/fix/455/failures_on_django_master
Fixes issue 455 (incompatability with django master at 3.2)
2 parents 69bbe58 + fa6808e commit c005410

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Contributors
1313
* Abel Daniel
1414
* Adam Chainz
1515
* Adam Wentz
16+
* Adam Donaghy
1617
* Andrew Ingram (contributed setup.py)
1718
* Al Johri
1819
* Alex Alvarez

polymorphic/query.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import copy
66
from collections import defaultdict
77

8+
from django import get_version as get_django_version
89
from django.contrib.contenttypes.models import ContentType
910
from django.core.exceptions import FieldDoesNotExist
1011
from django.db.models.query import ModelIterable, Q, QuerySet
@@ -157,18 +158,34 @@ def not_instance_of(self, *args):
157158
# Implementation in _translate_polymorphic_filter_defnition."""
158159
return self.filter(not_instance_of=args)
159160

160-
def _filter_or_exclude(self, negate, *args, **kwargs):
161-
# We override this internal Django functon as it is used for all filter member functions.
162-
q_objects = translate_polymorphic_filter_definitions_in_args(
163-
self.model, args, using=self.db
164-
)
165-
# filter_field='data'
166-
additional_args = translate_polymorphic_filter_definitions_in_kwargs(
167-
self.model, kwargs, using=self.db
168-
)
169-
return super(PolymorphicQuerySet, self)._filter_or_exclude(
170-
negate, *(list(q_objects) + additional_args), **kwargs
171-
)
161+
# Makes _filter_or_exclude compatible with the change in signature introduced in django at 9c9a3fe
162+
if get_django_version() >= "3.2":
163+
def _filter_or_exclude(self, negate, args, kwargs):
164+
# We override this internal Django function as it is used for all filter member functions.
165+
q_objects = translate_polymorphic_filter_definitions_in_args(
166+
queryset_model=self.model, args=args, using=self.db
167+
)
168+
# filter_field='data'
169+
additional_args = translate_polymorphic_filter_definitions_in_kwargs(
170+
queryset_model=self.model, kwargs=kwargs, using=self.db
171+
)
172+
args = list(q_objects) + additional_args
173+
return super(PolymorphicQuerySet, self)._filter_or_exclude(
174+
negate=negate, args=args, kwargs=kwargs
175+
)
176+
else:
177+
def _filter_or_exclude(self, negate, *args, **kwargs):
178+
# We override this internal Django function as it is used for all filter member functions.
179+
q_objects = translate_polymorphic_filter_definitions_in_args(
180+
self.model, args, using=self.db
181+
)
182+
# filter_field='data'
183+
additional_args = translate_polymorphic_filter_definitions_in_kwargs(
184+
self.model, kwargs, using=self.db
185+
)
186+
return super(PolymorphicQuerySet, self)._filter_or_exclude(
187+
negate, *(list(q_objects) + additional_args), **kwargs
188+
)
172189

173190
def order_by(self, *field_names):
174191
"""translate the field paths in the args, then call vanilla order_by."""
@@ -521,4 +538,4 @@ def get_real_instances(self, base_result_objects=None):
521538
if not self.model.polymorphic_query_multiline_output:
522539
return olist
523540
clist = PolymorphicQuerySet._p_list_class(olist)
524-
return clist
541+
return clist

0 commit comments

Comments
 (0)