drf-extra-fields: 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:

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 ?

About this issue

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

Commits related to this issue

Most upvoted comments

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.

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

I can confirm that @ilmesi’s answer works. It seems like the most ideal work-around thus far.

It would be good if this was documented somewhere officially.

@ilmesi Thank you for the suggestion. This seems pretty simple to me compered to creating a FieldInspector. What do you think @WasinTh ?

@WasinTh Would you like to update your PR #100 with the suggested solution #66 (comment) ?

@alicertel Sure. I’ve pushed the updated answer on those PRs.

The model that I’ve created is identical to OP’s.

I think drf-yasg==1.13.0 is causing the problem here. I’ve just tried it with Base64ImageField and then I’ve used the built-in serializers.ImageField; this is what I’ve gotten in both cases:

yasg