如何使用 RetrieveAPIView 根据 Django Rest Framework 中的特定字段检索数据?

How to retrieve data based on specific field in Django Rest Framework using RetrieveAPIView?

使用下面的代码,我可以根据 id 检索数据,如何更新此代码以便我可以改为根据文件名检索数据?

我的网址是

urlpatterns = [
    path("items/<pk>", SingleUploadItem.as_view()),
]

我的观点是:

class SingleUploadItem(RetrieveAPIView):

    queryset = fileUpload.objects.all()
    serializer_class = fileUploadSerializer

我的模特是

class fileUpload(models.Model):
    fileName = models.CharField(max_length=200, unique=True, blank=True)

首先,在urls.py

urlpatterns = [
    path("items/<str:file_name>", SingleUploadItem.as_view()),
]

并在 views.py、

from rest_framework import status
from .models import fileUpload
from .serializers import FileUploadSerializer

class SingleUploadItem(RetrieveAPIView):

    queryset = fileUpload.objects.all()
    serializer_class = fileUploadSerializer

    def get(self, request, file_name):
        try:
            fileupload_obj = fileUpload.objects.get(fileName = file_name)
            return Response(FileUploadSerializer(fileupload_obj).data)
        except fileUpload.DoesNotExist:
            return Response(status = status.HTTP_400_BAD_REQUEST)