0

I have a permissions.py file at my core app in django which I want to use its My_has_permission class for project level permissions in settings.py. note settings.py and permissions.py are in same folder. so I tried

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        '.permissions.My_has_permission',
    ]
}

but it gave this error: TypeError: the 'package' argument is required to perform a relative import for

so how can I have this package as my DEFAULT_PERMISSION_CLASSES?

here on Im explaining my tries:
  1. I want the opposite of what importlib.import_module('.a', package='b') does

  2. also make another ss.py and with

from .permissions import My_has_permission
print(My_has_permission.__file__)

tried to get the path but gave error: ImportError: attempted relative import with no known parent package, this error is not because djangorestframework is not installed. note I say this because I have used some imports from rest_framework. besides error is from from .permissions import My_has_permission in ss.py

so my question is it my permissions.py faulty?

from rest_framework import permissions

PERMS_MAP = {
    # 'OPTIONS': 'view_',
    'HEAD': 'view_',
    'GET': 'view_',
    'DELETE': 'delete_',
    'PATCH': 'change_',
    'POST': 'add_',
    'PUT': 'change_'
}
class My_has_permission(permissions.BasePermission):
    app_label = view.queryset.model._meta.app_label
    model_name = view.queryset.model._meta.model_name
    return request.user.has_perm(str(self.app_label+'.'+PERMS_MAP[request.method]+self.model_name)) or request.user.is_admin or request.user.is_superuser

update

I moved permissions.py to another app(users) and I did

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'users.permissions.My_has_permission',
    ]
}

so solved the problem practically but problem with this trail(from same folder) remains unsolved.

Farhang Amaji
  • 742
  • 11
  • 24

0 Answers0