With the code below, I am able to retrieve data based on id
, how can I update this code so I can retrieve data based on fileName instead?
My urls is
urlpatterns = [
path("items/<pk>", SingleUploadItem.as_view()),
]
My views is:
class SingleUploadItem(RetrieveAPIView):
queryset = fileUpload.objects.all()
serializer_class = fileUploadSerializer
My model is
class fileUpload(models.Model):
fileName = models.CharField(max_length=200, unique=True, blank=True)
CodePudding user response:
First, in urls.py
urlpatterns = [
path("items/<str:file_name>", SingleUploadItem.as_view()),
]
And in 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)
Hope it could help.