Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Improved formatting.
Source Link
retro_coder
  • 466
  • 1
  • 13
  • 32

I have created a DRF API that allows me to submit an image using the POST method via POSTMAN. The image is not stored in the model. After it's submission, I want to view the image's name in the browser using the Django Rest Framework. After reading sources in the net, I found out that people used the GET method in order to view all the data in a model. However, I don't have a model (don't require it for now) so how can I implement this requirement?

The result should be something like this:

{ "image_name": <"name_of_the_image_stored"> } 

This is what I had done so far:

from rest_framework.views import APIView from rest_framework.response import Response from .serializers import ImgSerializer from rest_framework import status from rest_framework.parsers import FileUploadParser class ImageAPI(APIView):   defparser_classes post= (selfFileUploadParser, request):   parser_classes =def post(FileUploadParserself, request): #key is 'image' when uploading in POSTMAN file = self.request.data.get('image',   None) data = file['image'] if filedata: uploaded_file = filedata fs = FileSystemStorage(location=settings.PRIVATE_STORAGE_ROOT) filename = fs.save(uploaded_file.name, uploaded_file) data = [{"image_name": filename}] serializer = ImgSerializer(data, many = True).data return Response(serializer, status = 200) else: return Response("Please upload an image", status = 400) def get(self, request): #What should I do here in order to view the submitted image above? 

serializers.py:

from rest_framework import serializers class ImgSerializer(serializers.Serializer): image_name = serializers.CharField() 

urls.py:

from upload.views import ImageAPI from rest_framework.urlpatterns import format_suffix_patterns urlpatterns = [ path("api/", ImageAPI.as_view(), name = "api"), ] urlpatterns = format_suffix_patterns(urlpatterns) 

I have created a DRF API that allows me to submit an image using the POST method via POSTMAN. The image is not stored in the model. After it's submission, I want to view the image's name in the browser using the Django Rest Framework. After reading sources in the net, I found out that people used the GET method in order to view all the data in a model. However, I don't have a model (don't require it for now) so how can I implement this requirement?

The result should be something like this:

{ "image_name": <"name_of_the_image_stored"> } 

This is what I had done so far:

from rest_framework.views import APIView from rest_framework.response import Response from .serializers import ImgSerializer from rest_framework import status from rest_framework.parsers import FileUploadParser class ImageAPI(APIView):   def post(self, request):   parser_classes = (FileUploadParser,) #key is 'image' when uploading in POSTMAN file = request.data.get('image', None) if file: uploaded_file = file fs = FileSystemStorage(location=settings.PRIVATE_STORAGE_ROOT) filename = fs.save(uploaded_file.name, uploaded_file) data = [{"image_name": filename}] serializer = ImgSerializer(data, many = True).data return Response(serializer, status = 200) else: return Response("Please upload an image", status = 400) def get(self, request): #What should I do here in order to view the submitted image above? 

serializers.py:

from rest_framework import serializers class ImgSerializer(serializers.Serializer): image_name = serializers.CharField() 

urls.py:

from upload.views import ImageAPI from rest_framework.urlpatterns import format_suffix_patterns urlpatterns = [ path("api/", ImageAPI.as_view(), name = "api"), ] urlpatterns = format_suffix_patterns(urlpatterns) 

I have created a DRF API that allows me to submit an image using the POST method via POSTMAN. The image is not stored in the model. After it's submission, I want to view the image's name in the browser using the Django Rest Framework. After reading sources in the net, I found out that people used the GET method in order to view all the data in a model. However, I don't have a model (don't require it for now) so how can I implement this requirement?

The result should be something like this:

{ "image_name": <"name_of_the_image_stored"> } 

This is what I had done so far:

from rest_framework.views import APIView from rest_framework.response import Response from .serializers import ImgSerializer from rest_framework import status from rest_framework.parsers import FileUploadParser class ImageAPI(APIView): parser_classes = (FileUploadParser,) def post(self, request): #key is 'image' when uploading in POSTMAN file = self.request.data    data = file['image'] if data: uploaded_file = data fs = FileSystemStorage(location=settings.PRIVATE_STORAGE_ROOT) filename = fs.save(uploaded_file.name, uploaded_file) data = [{"image_name": filename}] serializer = ImgSerializer(data, many = True).data return Response(serializer, status = 200) else: return Response("Please upload an image", status = 400) def get(self, request): #What should I do here in order to view the submitted image above? 

serializers.py:

from rest_framework import serializers class ImgSerializer(serializers.Serializer): image_name = serializers.CharField() 

urls.py:

from upload.views import ImageAPI from rest_framework.urlpatterns import format_suffix_patterns urlpatterns = [ path("api/", ImageAPI.as_view(), name = "api"), ] urlpatterns = format_suffix_patterns(urlpatterns) 
Source Link
retro_coder
  • 466
  • 1
  • 13
  • 32

How to view API data within a GET method that's created using POST method in Django (without a model)?

I have created a DRF API that allows me to submit an image using the POST method via POSTMAN. The image is not stored in the model. After it's submission, I want to view the image's name in the browser using the Django Rest Framework. After reading sources in the net, I found out that people used the GET method in order to view all the data in a model. However, I don't have a model (don't require it for now) so how can I implement this requirement?

The result should be something like this:

{ "image_name": <"name_of_the_image_stored"> } 

This is what I had done so far:

from rest_framework.views import APIView from rest_framework.response import Response from .serializers import ImgSerializer from rest_framework import status from rest_framework.parsers import FileUploadParser class ImageAPI(APIView): def post(self, request): parser_classes = (FileUploadParser,) #key is 'image' when uploading in POSTMAN file = request.data.get('image', None) if file: uploaded_file = file fs = FileSystemStorage(location=settings.PRIVATE_STORAGE_ROOT) filename = fs.save(uploaded_file.name, uploaded_file) data = [{"image_name": filename}] serializer = ImgSerializer(data, many = True).data return Response(serializer, status = 200) else: return Response("Please upload an image", status = 400) def get(self, request): #What should I do here in order to view the submitted image above? 

serializers.py:

from rest_framework import serializers class ImgSerializer(serializers.Serializer): image_name = serializers.CharField() 

urls.py:

from upload.views import ImageAPI from rest_framework.urlpatterns import format_suffix_patterns urlpatterns = [ path("api/", ImageAPI.as_view(), name = "api"), ] urlpatterns = format_suffix_patterns(urlpatterns)