2

I have created Rest API using Django rest-framework, when I am trying to test in with Swagger, it is not supporting FileField. in place of file uploader, it is showing text field.

Here What I did -

models.py

class Document(models.Model):
    IS_DELETED = (
        (True, True),
        (False, False),
    )

    project = models.ForeignKey(Project, null=False, blank=False, on_delete=models.CASCADE)
    document_file = models.FileField(upload_to=PROJECT_DOC_PATH, null=False, blank=False)
    is_deleted = models.BooleanField(choices=IS_DELETED, default=False)
    created_by = models.CharField(max_length=500, null=True, blank=True)
    created_on = models.DateTimeField(null=False, blank=False, default=timezone.now)
    updated_by = models.CharField(max_length=500, null=True, blank=True)
    updated_on = models.DateTimeField(null=True, blank=True)

serializers.py

class DocumentSerializer(serializers.ModelSerializer):

    class Meta:
        model = DocumentTbl
        fields = ('project','document_file')

views.py

@method_decorator(csrf_exempt, name='dispatch')
class Document(generics.CreateAPIView):
    renderer_classes = (JSONRenderer,)
    parser_classes = (FormParser, MultiPartParser,)
    permission_classes = (permissions.AllowAny, IsAuthenticated)
    serializer_class = DocumentSerializer    

    def post(self, request):
        try:
            serializer = DocumentSerializer(data=request.data)
            if serializer.is_valid():
                serializer.create(serializer.validated_data)
                return Response({
                    messages.RESULT: messages.SUCCESS,
                    messages.MESSAGE: messages.SUCCESSFULLY_DATA_SAVE,
                }, status=status.HTTP_200_OK)
            else:
                return Response({
                    messages.RESULT: messages.FAIL,
                    messages.RESPONSE_DATA: serializer.errors,
                    messages.MESSAGE: messages.INVALID_DATA,
                }, status=status.HTTP_400_BAD_REQUEST)
        except Exception as ex:
            return Response({
                messages.RESULT: messages.FAIL,
                messages.MESSAGE: messages.SERVER_ERROR + ', due to {}'.format(str(ex)),
            }, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

urls.py

urlpatterns = [
    path('document/', Document.as_view(),name='document'),
]

req.txt

Python==3.x
Django==2.2.5
django-cors-headers==3.1.0
django-rest-swagger==2.2.0
djangorestframework==3.10.3
djangorestframework-simplejwt==4.3.0

I have tried Django REST Framework + Django REST Swagger + ImageField but didn't solve my issue.

Output enter image description here

Vikram Singh Chandel
  • 1,290
  • 2
  • 17
  • 36

1 Answers1

0

This is a known issue at Django-Rest-Swagger

you can refer to below url: https://github.com/marcgibbons/django-rest-swagger/issues/647

the rootcause is Core api doesn't have a File type yet, it is marked as # LATER: File

see https://github.com/core-api/python-coreschema/blob/master/coreschema/schemas.py#L24