How to restrict Django Rest Framework browsable API interface to admin users

前端 未结 3 647
执笔经年
执笔经年 2021-02-07 08:36

I\'m developing a Django Rest Framework backend for a mobile app. The API is private and will only ever be used internally.

The browsable API is convenient for helping d

3条回答
  •  梦毁少年i
    2021-02-07 08:54

    Is `DEFAULT_PERMISSION_CLASSES' setting not enough? This sets a default restriction on all views DRF docs on default permission classes

    In settings.py:

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

    They will 'reach' the browsable interface but all types of requests will be denied if not authorized.

    If for some reason various end-points needed to be reached by non-admin users, you could loosen the restriction on a view-by-view basis.

提交回复
热议问题