question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to make the ImageField as required in Swagger POST API

See original GitHub issue

My model:

class Image(models.Model):
      image=models.ImageField(upload_to='photos')
      name=models.CharField(max_length=40,unique=False)

My serializer:

class imagesSerializer(serializers.ModelSerializer):
      image = Base64ImageField(required=True)
      class Meta:
         model = Image
         fields = ('id','name','image')

My swagger view: kfrof

How to make the imagefield as required in swagger post request ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:29 (19 by maintainers)

github_iconTop GitHub Comments

8reactions
ilmesicommented, May 11, 2020

In Swagger API, I’m using the following setup to show the field as required without any problems:

class PDFBase64FileField(Base64FileField):
    ALLOWED_TYPES = ['pdf']

    class Meta:
        swagger_schema_fields = {
            'type': 'string',
            'title': 'File Content',
            'description': 'Content of the file base64 encoded',
            'read_only': False  # <-- FIX
        }

    def get_file_extension(self, filename, decoded_file):
        try:
            PyPDF2.PdfFileReader(io.BytesIO(decoded_file))
        except PyPDF2.utils.PdfReadError as e:
            logger.warning(e)
        else:
            return 'pdf'

And to use “string” format instead of an URL, you will need to create the field with use_url=False.

3reactions
Hassanzadeh-sdcommented, Nov 25, 2020

you should change your parser like this :

    from rest_framework import permissions, viewsets
    from rest_framework.mixins import (CreateModelMixin, DestroyModelMixin,
                                       ListModelMixin, RetrieveModelMixin)
    from rest_framework.parsers import FormParser, MultiPartParser
    from .models import Customer
    from .permissions import CustomerPermission
    from .serializer import CustomerSerializer
    
    
    class CustomerViewSet(CreateModelMixin, ListModelMixin, RetrieveModelMixin, 
                          DestroyModelMixin, viewsets.GenericViewSet):
        permission_classes = [CustomerPermission]
        queryset = Customer.objects.all()
        serializer_class = CustomerSerializer
        parser_classes = (FormParser, MultiPartParser)

after that you can check your swagger :

image

I hope this can help you

Read more comments on GitHub >

github_iconTop Results From Across the Web

drf-yasg: Image field not displaying in swagger ui
I am new to django-rest-framework. I am using DRF and drf-yasg Swagger. Can any body help to upload image from request body? I...
Read more >
Unable to make the ImageField as required in Swagger POST API
My model: class Image(models.Model): image=models.ImageField(upload_to='photos') name=models.CharField(max_length=40,unique=False). My serializer:
Read more >
Media Types - Swagger
Media type is a format of a request or response body data. Web service operations can accept and return data in different formats,...
Read more >
Insightly API v3.1 Help
Review each object's endpoints to understand how to get the data and make changes to the records. For example, to get the links...
Read more >
Upload image or file using Django Rest Framework ... - YouTube
How to write REST API for uploading image and file using Django rest framework, How to post file or image from postman to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found