Object does not exist

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

If you are looking for assistance here, you have not provided any information for us to help you.

See this Stack Overflow guide on asking good questions: https://stackoverflow.com/help/how-to-ask It doesn’t completely apply, but it’s a start.

You’ll need to post the complete traceback of the error you’re receiving, along with the code (view, model, etc) where the error is occurring. You’ll probably need to include other relevant information as well, such as the data being referenced.

When posting code (or tracebacks, etc) here, enclose the code (or traceback) between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted - critical with Python.

Thank you for the guidance.
I hope the update explains my problem properly.

So to verify this, if you try these from within the Django shell:

AudioRoomMember.objects.get(uid=99, slug='test')
AudioRoomMember.objects.get(uid=377, slug='test')

They both work?

I figured that autoslug field adds ‘-1’ if the room_name field is the same name.

It’s now fixed.

Thank for the help.