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

Import of rest_framework backend can fail #567

Closed
tarkatronic opened this issue Nov 18, 2016 · 15 comments
Closed

Import of rest_framework backend can fail #567

tarkatronic opened this issue Nov 18, 2016 · 15 comments
Assignees
Labels

Comments

@tarkatronic
Copy link

In the DRF docs, they give an example which looks like:

import django_filters
# ...

class ProductFilter(django_filters.rest_framework.FilterSet):
    # ...

This does not work, however, with the way the django_filters/__init__.py is set up. Using this example, I get:

AttributeError: 'module' object has no attribute 'rest_framework'

I have found a fix for it, by adding the following to django_filters/__init__.py:

from . import rest_framework

@carltongibson
Copy link
Owner

Yeah. That's probably right. Fancy doing a PR?

@tarkatronic
Copy link
Author

Sure thing, I'll have it to you shortly.

@rpkilby
Copy link
Collaborator

rpkilby commented Nov 18, 2016

There is a problem in that the rest_framework sub-package has a dependency on DRF. If DRF isn't installed, then django_filters will not be importable.

One possible way to get around this is to add a compat.rest_framework check, and then only import the rest_framework sub-package if it passes.

@rpkilby
Copy link
Collaborator

rpkilby commented Nov 18, 2016

Alternatively, the docs could be updated to include the correct import

import django_filters.rest_framework

class ProductFilter(django_filters.rest_framework.FilterSet):
    # ...

@carltongibson
Copy link
Owner

I think import django_filters is my preferred API.

One possible way to get around this is to add a compat.rest_framework check, and then only import the rest_framework sub-package if it passes.

Yes. This.

@marcinn
Copy link

marcinn commented Nov 24, 2016

Why django_filters have references to DRF and Crispy? Both aren't a dependecies. Adapter for DRF should be provided inside DRF or some DRF' plugin/extension.

Why? AFAIK django_filter is not an DRF's extension and there are other REST frameworks/libraries.It is not reasonable to include their adapters here. Why DRF is an exception? Same notes for Crispy.

@carltongibson
Copy link
Owner

@marcinn Neither DRF nor Django-Filters depend on each other. There is an optional combined usage.

That code used to live in DRF. It now lives in Django-Filters. That's all.

Why move? Less surface-area for DRF and easier integration for Django-Filter related fixes. e.g. django_filters.rest_framework.filters.BooleanFilter — this is code that has no place in DRF.

carltongibson pushed a commit that referenced this issue Nov 25, 2016
* Try importing `rest_framework` module

Fixes #567

* Correct Typo

Thanks @kevin-brown!
@decibyte
Copy link

decibyte commented Feb 5, 2017

I'm getting some unexpected behavior around this. I do have DRF installed. But rest_framework gets set to None anyway. If I remove the try...except, things work as expected, without any ImportErrors.

Anyone have an idea what is going on?

@carltongibson
Copy link
Owner

Hmm. No idea at all at first glance. 😐

Can you put a breakpoint in and examine the environment where the import is failing? (Hopefully that reveals more)

@decibyte
Copy link

decibyte commented Feb 6, 2017

Thanks for a quick response. I'm having a tight deadline right now, but I'll return to the issue when I find some time for this.

@oliver-zhou
Copy link

When trying to add django_filters to INSTALLED_APPS in a DRF based project, I get an import issue that doesn't exhibit itself when imported normally. django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

@carltongibson
Copy link
Owner

@oliver-zhou I guess that it's unlikely to be related to this issue. Most likely you're accessing some model attribute in your FilterSet definition — which is processed at import time before the AppRegistry is initialised.

You'll get a traceback. Look through it. That will point to the exact place you're getting the error. Either move the offending line so that it's called at runtime or else (if that's really unavoidable) move the offending definition into your app's ready handler.

I hope that helps. If you're still stuck I suggest posting a question (with the traceback and offending definitions) on StackOverflow.

@osonwanne
Copy link

osonwanne commented Jul 6, 2017

@tarkatronic That didn't work for me, still getting error:

DFBase = django_filters.rest_framework.DjangoFilterBackend
AttributeError: 'module' object has no attribute 'rest_framework'

@osonwanne
Copy link

Changing django-filter in requirements.txt from 1.0.1 to 1.0.0 fixed it for me.

@rpkilby
Copy link
Collaborator

rpkilby commented Jul 11, 2017

@osonwanne - That would make sense, since the import was added in the 1.0.1 release. Could you try installing the branch from #741? This shouldn't mask the import error and might give you a useful traceback.

auvipy pushed a commit to chibisov/drf-extensions that referenced this issue Dec 12, 2017
* Parse rest-framework's version by using more tolerant functions

* Factor the way the django-rest-framework's version is parsed

* django-rest-framework 3.7 dropped support for django 1.8

* Fix wrong import from rest_framework.filters

See: <carltongibson/django-filter#567>

* Add tests for regression on #198
ddohler pushed a commit to azavea/climate-change-api that referenced this issue Aug 21, 2023
Ensure django-filters dependency DRF import happens before filters import attempted.

To prevent carltongibson/django-filter#567.
ddohler pushed a commit to azavea/climate-change-api that referenced this issue Aug 21, 2023
Ensure django-filters dependency DRF import happens before filters import attempted.

To prevent carltongibson/django-filter#567.
ddohler pushed a commit to azavea/climate-change-api that referenced this issue Aug 21, 2023
Ensure django-filters dependency DRF import happens before filters import attempted.

To prevent carltongibson/django-filter#567.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants