Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issue 455 (incompatability with django master at 3.2) #461

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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