In some objects, Django gives me does not exist but they exist in the database.
the model:
class AudioRoomMember(models.Model):
uid = models.CharField(max_length=100)
room_name = models.CharField(max_length=200)
slug = AutoSlugField(
populate_from='room_name',
unique_with='id',
blank=True
)
in_session = models.BooleanField(default=False)
def __str__(self):
return f'User UID: {self.uid} - @{self.room_name}'
The view:
class RoomDetailView(APIView):
serializer_class = AudioRoomMemberSerializer
def get(self, request, uid, slug, format=None):
room = AudioRoomMember.objects.get(uid=uid, slug=slug)
serializer = AudioRoomMemberSerializer(room)
return Response(serializer.data)
def put(self, request, uid, slug, format=None):
room = AudioRoomMember.objects.get(uid=uid, slug=slug)
serializer = AudioRoomMemberSerializer(room, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, uid, slug, format=None):
room = AudioRoomMember.objects.get(uid=uid, slug=slug)
room.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
and URL:
path(
'rooms/<int:uid>/<slug:slug>',
views.RoomDetailView.as_view(),
name="room_detail"
),
The problem is when I send a PUT or DELETE request for the first object the request works, but when I send the same requests I get:
[11/Jul/2022 14:55:33] "GET /api/get_token/?channel=TEST HTTP/1.1" 200 160
[11/Jul/2022 14:55:34] "GET /api/rooms/ HTTP/1.1" 200 70
[11/Jul/2022 14:55:37] "POST /api/create_member/ HTTP/1.1" 201 71
[11/Jul/2022 14:55:39] "GET /api/get_token/?channel=TEST HTTP/1.1" 200 161
[11/Jul/2022 14:55:39] "GET /api/rooms/ HTTP/1.1" 200 142
[11/Jul/2022 14:55:42] "OPTIONS /api/rooms/377/test HTTP/1.1" 200 0
[11/Jul/2022 14:55:42] "OPTIONS /api/rooms/99/test HTTP/1.1" 200 0
[11/Jul/2022 14:55:42] "POST /api/create_member/ HTTP/1.1" 201 74
[11/Jul/2022 14:55:42] "PUT /api/rooms/99/test HTTP/1.1" 200 70
Internal Server Error: /api/rooms/377/test
Traceback (most recent call last):
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/sudoer/projects/rousseau/api/views.py", line 63, in put
room = AudioRoomMember.objects.get(uid=uid, slug=slug)
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/django/db/models/query.py", line 435, in get
raise self.model.DoesNotExist(
api.models.AudioRoomMember.DoesNotExist: AudioRoomMember matching query does not exist.
[11/Jul/2022 14:55:42] "PUT /api/rooms/377/test HTTP/1.1" 500 104361
[11/Jul/2022 14:55:43] "OPTIONS /api/rooms/377/test HTTP/1.1" 200 0
Internal Server Error: /api/rooms/377/test
Traceback (most recent call last):
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/sudoer/projects/rousseau/api/views.py", line 63, in put
room = AudioRoomMember.objects.get(uid=uid, slug=slug)
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/sudoer/.local/share/virtualenvs/rousseau-rUJ26Ryu/lib/python3.10/site-packages/django/db/models/query.py", line 435, in get
raise self.model.DoesNotExist(
api.models.AudioRoomMember.DoesNotExist: AudioRoomMember matching query does not exist.
[11/Jul/2022 14:55:43] "PUT /api/rooms/377/test HTTP/1.1" 500 104361