使用下面的代碼,我可以根據 檢索資料id,如何更新此代碼以便可以根據 fileName 檢索資料?
我的網址是
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)
uj5u.com熱心網友回復:
首先,在 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)
希望它可以幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/479986.html
標籤:django django-rest-framework django-views
下一篇:在PHP的幫助下進行隨機重定向
