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

I got an error pasting the example OpenApiAuthenticationExtension into my settings file, it complained about circular imports. #307

Closed
tfranzel opened this issue Feb 18, 2021 · 4 comments

Comments

@tfranzel
Copy link
Owner

I got an error pasting the example OpenApiAuthenticationExtension into my settings file, it complained about circular imports.

  File "/Users/mish/xxxxx/permissions/__init__.py", line 1, in <module>
    from drf_spectacular.openapi import OpenApiAuthenticationExtension
  File "/Users/mish/Library/Caches/pypoetry/virtualenvs/pds-Vl2mZPzh-py3.9/lib/python3.9/site-packages/drf_spectacular/openapi.py", line 12, in <module>
    from rest_framework.generics import GenericAPIView
  File "/Users/mish/Library/Caches/pypoetry/virtualenvs/pds-Vl2mZPzh-py3.9/lib/python3.9/site-packages/rest_framework/generics.py", line 24, in <module>
    class GenericAPIView(views.APIView):
AttributeError: partially initialized module 'rest_framework.views' has no attribute 'APIView' (most likely due to a circular import)

Originally posted by @revmischa in #264 (comment)

@tfranzel
Copy link
Owner Author

@revmischa i moved this here as it is a different issue. can you give a bit more details?

which example pasted where?

@revmischa
Copy link

Ah sorry, I pasted the example

class MyAuthenticationScheme(OpenApiAuthenticationExtension):
    target_class = 'my_app.MyAuthentication'  # full import path OR class ref
    name = 'MyAuthentication'  # name used in the schema

    def get_security_definition(self, auto_schema):
        return {
            'type': 'apiKey',
            'in': 'header',
            'name': 'api_key',
        }

Into my settings/base.py file and got that error.

@tfranzel
Copy link
Owner Author

ok so i'm pretty sure that importing into settings.py is not the best idea. the documentation says this but it should have been "import this anywhere except settings.py (and derivatives)".

loading that class likely triggers retrieval of settings, while those are not loaded yet as you are still parsing settings.py.

personally, i just put all that stuff in proj/schema.py and do a from proj.schema import * in urls.py and be done with it. i should adapt the documentation.

@tfranzel
Copy link
Owner Author

improved the documentation. hope this works now for you. i'll close but feel free to circle back

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

2 participants