Django app getting the error "TypeError: 'dict_keys' object is not subscriptable"
Question:
Here is the views:
class UserViewSet(viewsets.ViewSet):
def list(self, request):
queryset = User.objects.all()
serializer = UserSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
queryset = User.objects.all()
user = get_object_or_404(queryset, pk=pk)
serializer = UserSerializer(user)
return Response(serializer.data)
urls:
from django.urls import path
from .views import *
from rest_framework import routers
userl = UserViewSet.as_view({'get': 'list'})
userd = UserViewSet.as_view({'get': 'retrieve'})
router = routers.SimpleRouter()
router.register(r'users', UserViewSet, basename='user')
urlpatterns = [
path('users/', userl, name='user-list'),
path('users/<int:pk>/', userd, name='user-detail'),
path('register',RegisterUserAPIView.as_view()),
path('login/', LoginAPIView.as_view()),
path('logout', LogoutAPIView.as_view()),
path('products', ProductsViewSet.as_view({'get': 'list'})),
]'login/', LoginAPIView.as_view()),
path('logout', LogoutAPIView.as_view()),
path('products', ProductsViewSet.as_view({'get': 'list'})),
]
Traceback:
File "C:UsersUserDesktoperkin2venvlibsite-packagesroutersrouter.py", line 27, in db_for_read
return settings.DATABASES.keys()[0]
TypeError: 'dict_keys' object is not subscriptable
I’ve tried using keys()
but then it says:
function has no attribute keys().
Answers:
Apparently and perhaps counter-intuitively, the keys()
method for dictionaries does not return a list of keys. Let’s check:
d = {'a': 1, 'b': 2}
print(d.keys())
>>> dict_keys(['a', 'b'])
print(type(d.keys()))
>>> <class 'dict_keys'>
d.keys()[0]
>>> TypeError: 'dict_keys' object is not subscriptable
This was not always so. In Python 2, the keys()
method did return a list. See this old question.
To get an actual list of the keys, you can call list()
on the dictionary:
print(list(d))
>>> ['a', 'b']
Then indexing works as expected:
print(list(d)[0])
>>> a
So you may want to change the line return settings.DATABASES.keys()[0]
to this:
return list(settings.DATABASES)[0]
Here is the views:
class UserViewSet(viewsets.ViewSet):
def list(self, request):
queryset = User.objects.all()
serializer = UserSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
queryset = User.objects.all()
user = get_object_or_404(queryset, pk=pk)
serializer = UserSerializer(user)
return Response(serializer.data)
urls:
from django.urls import path
from .views import *
from rest_framework import routers
userl = UserViewSet.as_view({'get': 'list'})
userd = UserViewSet.as_view({'get': 'retrieve'})
router = routers.SimpleRouter()
router.register(r'users', UserViewSet, basename='user')
urlpatterns = [
path('users/', userl, name='user-list'),
path('users/<int:pk>/', userd, name='user-detail'),
path('register',RegisterUserAPIView.as_view()),
path('login/', LoginAPIView.as_view()),
path('logout', LogoutAPIView.as_view()),
path('products', ProductsViewSet.as_view({'get': 'list'})),
]'login/', LoginAPIView.as_view()),
path('logout', LogoutAPIView.as_view()),
path('products', ProductsViewSet.as_view({'get': 'list'})),
]
Traceback:
File "C:UsersUserDesktoperkin2venvlibsite-packagesroutersrouter.py", line 27, in db_for_read
return settings.DATABASES.keys()[0]
TypeError: 'dict_keys' object is not subscriptable
I’ve tried using keys()
but then it says:
function has no attribute keys().
Apparently and perhaps counter-intuitively, the keys()
method for dictionaries does not return a list of keys. Let’s check:
d = {'a': 1, 'b': 2}
print(d.keys())
>>> dict_keys(['a', 'b'])
print(type(d.keys()))
>>> <class 'dict_keys'>
d.keys()[0]
>>> TypeError: 'dict_keys' object is not subscriptable
This was not always so. In Python 2, the keys()
method did return a list. See this old question.
To get an actual list of the keys, you can call list()
on the dictionary:
print(list(d))
>>> ['a', 'b']
Then indexing works as expected:
print(list(d)[0])
>>> a
So you may want to change the line return settings.DATABASES.keys()[0]
to this:
return list(settings.DATABASES)[0]