diff --git a/HISTORY.rst b/HISTORY.rst index 072ec6a..cff4ca7 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -3,6 +3,16 @@ History ------- +0.3.3 (2017-06-15) +~~~~~~~~~~~~~~~~~~ + +* Fixed bug which did not allow to use SQLAlchemy backend fully + without having ``django.contrib.contenttypes`` in installed apps. + See `#36 `_. +* Improved SQLAlchemy versions compatibility. +* Added ``URLFilterBackend`` alias in DRF integration for backend to reduce + confusion with ``DjangoFilterBackend`` as in url filter core backend. + 0.3.2 (2017-05-19) ~~~~~~~~~~~~~~~~~~ diff --git a/url_filter/__init__.py b/url_filter/__init__.py index a725b33..7762f3f 100644 --- a/url_filter/__init__.py +++ b/url_filter/__init__.py @@ -4,4 +4,4 @@ __author__ = 'Miroslav Shubernetskiy' __email__ = 'miroslav@miki725.com' -__version__ = '0.3.2' +__version__ = '0.3.3' diff --git a/url_filter/backends/sqlalchemy.py b/url_filter/backends/sqlalchemy.py index 09c3a82..39a94ba 100644 --- a/url_filter/backends/sqlalchemy.py +++ b/url_filter/backends/sqlalchemy.py @@ -66,7 +66,7 @@ def get_model(self): """ Get the model from the given queryset """ - return self.queryset._primary_entity.entities[0] + return self.queryset._only_entity_zero().mapper.class_ def filter_by_specs(self, queryset): """ diff --git a/url_filter/filtersets/django.py b/url_filter/filtersets/django.py index 1f8421b..df4c5f2 100644 --- a/url_filter/filtersets/django.py +++ b/url_filter/filtersets/django.py @@ -3,7 +3,7 @@ import operator from django import forms -from django.contrib.contenttypes.fields import GenericForeignKey +from django.conf import settings from django.db import models from django.db.models.fields.related import ForeignObjectRel, RelatedField @@ -16,6 +16,13 @@ __all__ = ['ModelFilterSet', 'DjangoModelFilterSetOptions'] +GenericForeignKey = None + + +if 'django.contrib.contenttypes' in settings.INSTALLED_APPS: + from django.contrib.contenttypes.fields import GenericForeignKey + + MODEL_FIELD_OVERWRITES = SubClassDict({ models.AutoField: forms.IntegerField(min_value=0), models.FileField: lambda m: forms.CharField(max_length=m.max_length), @@ -95,7 +102,7 @@ def _build_filter(self, name, state): raise SkipFilter return self._build_filterset_from_reverse_field(field) - elif isinstance(field, GenericForeignKey): + elif GenericForeignKey and isinstance(field, GenericForeignKey): raise SkipFilter else: diff --git a/url_filter/integrations/drf.py b/url_filter/integrations/drf.py index d65e868..3957fc5 100644 --- a/url_filter/integrations/drf.py +++ b/url_filter/integrations/drf.py @@ -162,3 +162,6 @@ def filter_queryset(self, request, queryset, view): raise ValidationError(e.message_dict) return queryset + + +URLFilterBackend = DjangoFilterBackend