Skip to content

Commit

Permalink
Merge pull request #461 from AdamDonna/fix/455/failures_on_django_master
Browse files Browse the repository at this point in the history
Fixes issue 455 (incompatability with django master at 3.2)
  • Loading branch information
chrisglass committed Aug 21, 2020
2 parents 69bbe58 + fa6808e commit c005410
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Contributors
* Abel Daniel
* Adam Chainz
* Adam Wentz
* Adam Donaghy
* Andrew Ingram (contributed setup.py)
* Al Johri
* Alex Alvarez
Expand Down
43 changes: 30 additions & 13 deletions polymorphic/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import copy
from collections import defaultdict

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

def _filter_or_exclude(self, negate, *args, **kwargs):
# We override this internal Django functon as it is used for all filter member functions.
q_objects = translate_polymorphic_filter_definitions_in_args(
self.model, args, using=self.db
)
# filter_field='data'
additional_args = translate_polymorphic_filter_definitions_in_kwargs(
self.model, kwargs, using=self.db
)
return super(PolymorphicQuerySet, self)._filter_or_exclude(
negate, *(list(q_objects) + additional_args), **kwargs
)
# Makes _filter_or_exclude compatible with the change in signature introduced in django at 9c9a3fe
if get_django_version() >= "3.2":
def _filter_or_exclude(self, negate, args, kwargs):
# We override this internal Django function as it is used for all filter member functions.
q_objects = translate_polymorphic_filter_definitions_in_args(
queryset_model=self.model, args=args, using=self.db
)
# filter_field='data'
additional_args = translate_polymorphic_filter_definitions_in_kwargs(
queryset_model=self.model, kwargs=kwargs, using=self.db
)
args = list(q_objects) + additional_args
return super(PolymorphicQuerySet, self)._filter_or_exclude(
negate=negate, args=args, kwargs=kwargs
)
else:
def _filter_or_exclude(self, negate, *args, **kwargs):
# We override this internal Django function as it is used for all filter member functions.
q_objects = translate_polymorphic_filter_definitions_in_args(
self.model, args, using=self.db
)
# filter_field='data'
additional_args = translate_polymorphic_filter_definitions_in_kwargs(
self.model, kwargs, using=self.db
)
return super(PolymorphicQuerySet, self)._filter_or_exclude(
negate, *(list(q_objects) + additional_args), **kwargs
)

def order_by(self, *field_names):
"""translate the field paths in the args, then call vanilla order_by."""
Expand Down Expand Up @@ -521,4 +538,4 @@ def get_real_instances(self, base_result_objects=None):
if not self.model.polymorphic_query_multiline_output:
return olist
clist = PolymorphicQuerySet._p_list_class(olist)
return clist
return clist

0 comments on commit c005410

Please sign in to comment.