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

Async support for PermissionRequiredMixin #808

Open
g-nie opened this issue Nov 8, 2023 · 2 comments
Open

Async support for PermissionRequiredMixin #808

g-nie opened this issue Nov 8, 2023 · 2 comments

Comments

@g-nie
Copy link

g-nie commented Nov 8, 2023

I am trying to make use of PermissionRequiredMixin in an async context. My end goal is to be able to simply do:

from asgiref.sync import sync_to_async


class MyView(PermissionRequiredMixin, View):
    permission_required = 'some_perm'
    raise_exception = True

    async def get_permission_object(self):
        return await MyModel.objects.aget(slug='awesome-slug')

    async def get(self, request, *args, **kwargs):
        return await sync_to_async(render_to_string)('some_template.html')

but I'm currently (always) getting PermissionDenied with a console warning:
coroutine 'MyView.get_permission_object' was never awaited

If this is something that others can find useful as well, please do let me know and I can look into creating a PR for this. Thanks!

@panatale1
Copy link

@g-nie I think you need to await get_permission_object in the get method

@g-nie
Copy link
Author

g-nie commented Feb 4, 2024

Thanks for your feedback, I believe this would still not cover all possible cases here. Feel free to have a look on my PR, tests under AsyncPermissionRequiredMixinTests include all the cases that I could come up with.

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