question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Does django-rules work with django rest framework viewsets?

See original GitHub issue

Hi,

I am trying to integrate rules with DRF’s viewsets but I keep getting an AttributeError: 'OrgViewSet' object has no attribute 'request'

Here is my viewset code:

class OrgViewSet(PermissionRequiredMixin,
                 mixins.ListModelMixin,
                 viewsets.GenericViewSet):
    permission_required = CHANGE_ORG
    queryset = Org.objects.all()
    serializer_class = OrgSerializer

Here is the stack trace

Internal Server Error: /orgs/
Traceback (most recent call last):
  File "/Users/dre/code/treebeard/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/Users/dre/code/treebeard/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/dre/code/treebeard/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/dre/code/treebeard/venv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/dre/code/treebeard/venv/lib/python3.6/site-packages/rest_framework/viewsets.py", line 86, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/dre/code/treebeard/venv/lib/python3.6/site-packages/django/contrib/auth/mixins.py", line 90, in dispatch
    if not self.has_permission():
  File "/Users/dre/code/treebeard/venv/lib/python3.6/site-packages/rules/contrib/views.py", line 51, in has_permission
    return self.request.user.has_perms(perms, obj)
AttributeError: 'OrgViewSet' object has no attribute 'request'

Any help would be greatly appreciated.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
dfuncktcommented, Aug 11, 2019

rules now has built-in support for Django Rest Framework (contributed by @efficiosoft in #100). See here for docs on the feature: https://github.com/dfunckt/django-rules#permissions-in-django-rest-framework

DRF support ties well with the newly-added support for automatic permissions for models – see https://github.com/dfunckt/django-rules#permissions-in-models (also by @efficiosoft, in #99)

All in all, I think rules now integrates well with DRF – let me know of any feedback you may have.

3reactions
escodebarcommented, Nov 26, 2017

django-rules works with django REST framework viewsets using the django-rest-framework-rules package.

lnstallation and usage are documented in django-rest-framework-rules’ README, further examples can be found in django-rest-framework-rules’ tests.

@dfunckt: I guess, that you can close this issue now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Viewsets - Django REST framework
Django REST framework allows you to combine the logic for a set of related views in a single class, called a ViewSet ....
Read more >
django-rest-framework-rules - PyPI
django -rest-framework-rules aims to seamlessly integrate rules - a tiny but powerful app providing object-level permissions - into the Django REST framework ......
Read more >
DRF ViewSet operation authorization with rules - Stack Overflow
Using django-rules, how can the create() operation in CourseViewSet be restricted to only users of user_type TEACHER? python · security · django ......
Read more >
Built-in Permission Classes in Django REST Framework
This article looks at how the built-in permission classes work in Django REST Framework.
Read more >
Django Rest Framework ViewSets - Level Up Coding
A browsable API; Authentication; Serializers; Customization features. In this tutorial, we will build a Django application that utilizes ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found