Django 1.8 alpha support

This commit is contained in:
ellmetha
2015-01-28 00:41:34 +01:00
parent bf0d37a91c
commit 469d063cc9
4 changed files with 45 additions and 3 deletions
+15 -2
View File
@@ -6,6 +6,7 @@ from __future__ import absolute_import
from collections import defaultdict
import django
from django.db.models.query import QuerySet
from django.contrib.contenttypes.models import ContentType
from django.utils import six
@@ -95,10 +96,22 @@ class PolymorphicQuerySet(QuerySet):
def _process_aggregate_args(self, args, kwargs):
"""for aggregate and annotate kwargs: allow ModelX___field syntax for kwargs, forbid it for args.
Modifies kwargs if needed (these are Aggregate objects, we translate the lookup member variable)"""
def patch_lookup(a):
if django.VERSION < (1, 8):
a.lookup = translate_polymorphic_field_path(self.model, a.lookup)
else:
# With Django > 1.8, the field on which the aggregate operates is
# stored inside a query expression.
a.source_expressions[0].name = translate_polymorphic_field_path(
self.model, a.source_expressions[0].name)
get_lookup = lambda a: a.lookup if django.VERSION < (1, 8) else a.source_expressions[0].name
for a in args:
assert not '___' in a.lookup, 'PolymorphicModel: annotate()/aggregate(): ___ model lookup supported for keyword arguments only'
assert '___' not in get_lookup(a), 'PolymorphicModel: annotate()/aggregate(): ___ model lookup supported for keyword arguments only'
for a in six.itervalues(kwargs):
a.lookup = translate_polymorphic_field_path(self.model, a.lookup)
patch_lookup(a)
def annotate(self, *args, **kwargs):
"""translate the polymorphic field paths in the kwargs, then call vanilla annotate.
+9 -1
View File
@@ -7,7 +7,15 @@ from __future__ import absolute_import
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.db.models import Q, FieldDoesNotExist
from django.db.models.related import RelatedObject
try:
from django.db.models.related import RelatedObject
except ImportError:
# django.db.models.related.RelatedObject was replaced
# by django.db.models.fields.related.ForeignObjectRel in
# Django 1.8
from django.db.models.fields.related import ForeignObjectRel
RelatedObject = ForeignObjectRel
from functools import reduce