djangorestframeworks

i can not solve 'set' object is not subscriptable in django rest framework


My django project stopped working recently and is showing me 'set' object is not subscriptable error . I want to know how this error can be solved

Here is my 'urls.py'

'''
//this is the urls.py and the error arises from here## 

Heading

## from django.contrib import admin from django.urls import path,include from rest_framework import routers from hotel.views import BookedRoomsViewSet,roomsViewSet,userViewSet,eventViewSet

router = routers.DefaultRouter()
router.register(r'BookedRooms',BookedRoomsViewSet)
router.register(r'Rooms',roomsViewSet)
router.register(r'Users',userViewSet)
router.register(r'Events',eventViewSet)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include(router.urls))
]

Here is the 'models.py' file

'''
    from django.db import models
from datetime import date
from django.utils import timezone

# Create your models here.//models.py
class Room(models.Model):

    Room_no = models.IntegerField()    
    Type = models.CharField(max_length=30)
    Booked = models.BooleanField(default= False)

class BookedRooms(models.Model):

    firstname = models.CharField(max_length=30)
    lastname = models.CharField(max_length=30)
    phonenumber = models.CharField(max_length=30)
    roomtype = models.CharField(max_length=20)
    room = models.ForeignKey(Room,on_delete = models.CASCADE,null = True)

class Users(models.Model):

    username = models.CharField(max_length=30)
    email = models.EmailField(max_length=30)
    password = models.CharField(max_length=10) 

class Events(models.Model):

    name = models.CharField(max_length=100)
    details = models.TextField(max_length=300)
    startDate = models.CharField(max_length=12)
    startTime = models.TimeField()
    endDate = models.CharField(max_length=12)
    endTime = models.TimeField()


'''

Here is the 'serializers.py' file

''' 


from rest_framework import serializers
from .models import BookedRooms,Room,Users,Events

class RoomSerializers(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = BookedRooms
        fields = ('id','firstname','lastname','phonenumber','roomtype','room')

class roomSerializers(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Room
        fields = ('id','Room_no','Type','Booked')

class userSerializers(serializers.HyperlinkedModelSerializer):
    class Meta:
        model=Users
        fields= ('id','username','email','password')

class eventSerializers(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Events
        fields = ('id','name','details','startDate','startTime','endDate','endTime')

'''

Here is the 'views.py' file

''' 
    from django.shortcuts import 
from .models import BookedRooms,Room,Users,Events
from .serializers import RoomSerializers,roomSerializers,userSerializers,eventSerializers

from rest_framework import viewsets
from rest_framework.authentication import BasicAuthentication
from rest_framework.permissions import IsAuthenticated,
#from rest_framework.views import APIView

# Create your views here.

class BookedRoomsViewSet(viewsets.ModelViewSet):

    authentication_classes = (BasicAuthentication,)
    permission_classes = (IsAuthenticated,)
    queryset = BookedRooms.objects.all()
    serializer_class = RoomSerializers



class roomsViewSet(viewsets.ModelViewSet):

    authentication_classes = (BasicAuthentication,)
    permission_classes = (IsAuthenticated,)
    queryset = Room.objects.all()
    serializer_class = roomSerializers


class userViewSet(viewsets.ModelViewSet):

    authentication_classes = (BasicAuthentication,)
    permission_classes = (IsAuthenticated,)
    queryset = Users.objects.all()
    serializer_class  = userSerializers

class eventViewSet(viewsets.ModelViewSet):
    authentication_classes = (BasicAuthentication,)
    permission_classes = (IsAuthenticated,)
    queryset = Events.objects.all()
    serializer_class = eventSerializers

 '''

I would like if the answers are fast and here is the full traceback

'''


Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0000008516CD17B8>
Traceback (most recent call last):
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\autoreload.py", line 225, i
n wrapper
    fn(*args, **kwargs)
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\commands\runserve
r.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 37
9, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 36
6, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\checks\registry.py", line 71
, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\checks\urls.py", line 40, in
 check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\checks\urls.py", line 57, in
 _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\functional.py", line 37, in
 __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py", line 533, in
url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\functional.py", line 37, in
 __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py", line 526, in
urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "C:\users\jerry\documents\django-project\vuengo_env\vuengo\vuengo\urls.py", line 18, in <module>
    from rest_framework import routers
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\routers.py", line 25, in
<module>
    from rest_framework import RemovedInDRF311Warning, views
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\views.py", line 17, in <m
odule>
    from rest_framework.schemas import DefaultSchema
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\schemas\__init__.py", lin
e 33, in <module>
    authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\settings.py", line 213, i
n __getattr__
    val = self.user_settings[attr]
TypeError: 'set' object is not subscriptable


'''

Solution

  • I experienced the same error after I used comma (,) instead of colon (:) in settings.py REST_FRAMEWORK dictionary.

    This caused the error

    REST_FRAMEWORK = {
        'DEFAULT_PAGINATION_CLASS', 'rest_framework.pagination.PageNumberPagination', 
        'PAGE_SIZE', 10,
    }
    

    The above code is a python set, hence the error:

    val = self.user_settings[attr]
      ~~~~~~~~~~~~~~~~~~^^^^^^
    TypeError: 'set' object is not subscriptable
    

    This fixed my code

    REST_FRAMEWORK = {
        'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', 
        'PAGE_SIZE': 10,
    }
    

    Check if this is the case in any of the configuration directories in settings.py file.