Ansuman Bebarta Ansuman Bebarta - 6 months ago 15
Python Question

django rest framework no authentication no permission

I want to access api without any authentication or permission in django rest framework. To achieve this whenever I am having following setting I end up in error.

Setting:

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (),
'DEFAULT_AUTHENTICATION_CLASSES': (),
'PAGE_SIZE': 10
}


Error:

TypeError at /environments/

'many' is an invalid keyword argument for this function

Request Method: GET
Request URL: http://localhost:9900/environments/?format=api
Django Version: 1.8.3
Exception Type: TypeError
Exception Value:

'many' is an invalid keyword argument for this function

Exception Location: /home/anshumanb/.virtualenvs/aic_op_simple/lib/python3.4/site-packages/django/db/models/base.py in __init__, line 480
Python Executable: /home/anshumanb/.virtualenvs/aic_op_simple/bin/python
Python Version: 3.4.0
Python Path:

['/home/anshumanb/Projects/aic_op_simple/aic_gamma_backend_new',
'/home/anshumanb/.virtualenvs/aic_op_simple/lib/python3.4',
'/home/anshumanb/.virtualenvs/aic_op_simple/lib/python3.4/plat-x86_64-linux-gnu',
'/home/anshumanb/.virtualenvs/aic_op_simple/lib/python3.4/lib-dynload',
'/usr/lib/python3.4',
'/usr/lib/python3.4/plat-x86_64-linux-gnu',
'/home/anshumanb/.virtualenvs/aic_op_simple/lib/python3.4/site-packages']

Server time: Wed, 5 Aug 2015 13:46:35 +0000

Environment:


Request Method: GET
Request URL: http://localhost:9900/environments/?format=api

Django Version: 1.8.3
Python Version: 3.4.0
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'zone')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')


Traceback:
File "/home/anshumanb/.virtualenvs/aic_op_simple/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/anshumanb/.virtualenvs/aic_op_simple/lib/python3.4/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/home/anshumanb/.virtualenvs/aic_op_simple/lib/python3.4/site-packages/rest_framework/viewsets.py" in view
85. return self.dispatch(request, *args, **kwargs)
File "/home/anshumanb/.virtualenvs/aic_op_simple/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
456. response = self.handle_exception(exc)
File "/home/anshumanb/.virtualenvs/aic_op_simple/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
453. response = handler(request, *args, **kwargs)
File "/home/anshumanb/.virtualenvs/aic_op_simple/lib/python3.4/site-packages/rest_framework/mixins.py" in list
43. serializer = self.get_serializer(page, many=True)
File "/home/anshumanb/.virtualenvs/aic_op_simple/lib/python3.4/site-packages/rest_framework/generics.py" in get_serializer
109. return serializer_class(*args, **kwargs)
File "/home/anshumanb/.virtualenvs/aic_op_simple/lib/python3.4/site-packages/django/db/models/base.py" in __init__
480. raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])

Exception Type: TypeError at /environments/
Exception Value: 'many' is an invalid keyword argument for this function


But when I am using following setting it works but asking for authentication credentials.

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',),
'DEFAULT_AUTHENTICATION_CLASSES': (),
'PAGE_SIZE': 10
}


Please let me know how to access rest apis without any credentials.

I am running with django 1.8 djangorestframework 3.1

Answer

You can use following settings for DJR:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.AllowAny',),
    'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework.authentication.SessionAuthentication',),
}

AllowAny permission isn't really need, but I think it'll good practice to explicitly define that behavior in configuration.