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

Attribute Error when using rest_flex_fields.filter_backends.FlexFieldsFilterBackend #59

Open
hodossy opened this issue Jul 22, 2020 · 4 comments

Comments

@hodossy
Copy link

hodossy commented Jul 22, 2020

Thanks for this great library, I really love this. I have come across an issue however, when I wanted to use rest_flex_fields.filter_backends.FlexFieldsFilterBackend. When I added it to the configuration of DRF, the following import error occured:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "d:\workspace\my_project\api\.env\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "d:\workspace\my_project\api\.env\lib\site-packages\django\core\management\__init__.py", line 377, in execute
    django.setup()
  File "d:\workspace\my_project\api\.env\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "d:\workspace\my_project\api\.env\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "d:\workspace\my_project\api\.env\lib\site-packages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "d:\workspace\my_project\api\.env\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "d:\workspace\my_project\api\my_app\models.py", line 9, in <module>
    from utils.mixins import ResultsModelMixin, TrackFieldChangesMixin
  File "d:\workspace\my_project\api\utils\mixins\__init__.py", line 4, in <module>
    from .results import ResultsModelMixin, ResultsListViewMixin
  File "d:\workspace\my_project\api\utils\mixins\results\__init__.py", line 2, in <module>
    from .views import ResultsListViewMixin
  File "d:\workspace\my_project\api\utils\mixins\results\views.py", line 5, in <module>
    from rest_framework.generics import get_object_or_404
  File "d:\workspace\my_project\api\.env\lib\site-packages\rest_framework\generics.py", line 24, in <module>
    class GenericAPIView(views.APIView):
  File "d:\workspace\my_project\api\.env\lib\site-packages\rest_framework\generics.py", line 43, in GenericAPIView
    filter_backends = api_settings.DEFAULT_FILTER_BACKENDS
  File "d:\workspace\my_project\api\.env\lib\site-packages\rest_framework\settings.py", line 220, in __getattr__
    val = perform_import(val, attr)
  File "d:\workspace\my_project\api\.env\lib\site-packages\rest_framework\settings.py", line 168, in perform_import
    return [import_from_string(item, setting_name) for item in val]
  File "d:\workspace\my_project\api\.env\lib\site-packages\rest_framework\settings.py", line 168, in <listcomp>
    return [import_from_string(item, setting_name) for item in val]
  File "d:\workspace\my_project\api\.env\lib\site-packages\rest_framework\settings.py", line 177, in import_from_string
    return import_string(val)
  File "d:\workspace\my_project\api\.env\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "d:\workspace\my_project\api\.env\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "d:\workspace\my_project\api\.env\lib\site-packages\rest_flex_fields\__init__.py", line 3, in <module>
    from .views import FlexFieldsModelViewSet
  File "d:\workspace\my_project\api\.env\lib\site-packages\rest_flex_fields\views.py", line 6, in <module>
    from rest_framework import viewsets
  File "d:\workspace\my_project\api\.env\lib\site-packages\rest_framework\viewsets.py", line 199, in <module>
    class GenericViewSet(ViewSetMixin, generics.GenericAPIView):
AttributeError: module 'rest_framework.generics' has no attribute 'GenericAPIView'

Removing the imports from .views import FlexFieldsModelViewSet from __init__.py and from rest_framework.viewsets import GenericViewSet from filter_backends.py solved the issue for me. Probably the later could be placed behind the TYPE_CHECKING flag from typing, but I have no idea about the first one.

Env:

Django==3.0.7
django-filter==2.3.0
djangorestframework==3.11.0
drf-flex-fields==0.8.5
@hodossy
Copy link
Author

hodossy commented Jul 22, 2020

I will try to prepare a PR for this one.

@wolph
Copy link

wolph commented Oct 9, 2020

While I don't have a proper fix for the issue, you can work around it by manually specifying the filter backend:

import rest_flex_fields.filter_backends as flex_filters
from rest_framework import viewsets


class ModelViewSet(viewsets.ModelViewSet):
    filter_backends = viewsets.ModelViewSet.filter_backends + [
        flex_filters.FlexFieldsFilterBackend,
    ]

@wolph
Copy link

wolph commented Oct 9, 2020

I think this might be a duplicate of issue #46

@by-Exist
Copy link

I solved the problem by importing FlexFieldsFilterBackend directly from the settings.py module and inserting it into the backend settings.
However, I decided not to use that library due to the Schema format incompatible with drf_spectacular.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants