1

how can i handle base64 file field in django rest framework. I am using django extra fields but its not working.

serializers.py

from drf_extra_fields.fields import Base64FileField

class ProductSerializer(serializers.ModelSerializer):

    file  = Base64FileField()
    class Meta:
        model = Product

        fields = (
                    "name",
                    "file"
                )



class ProductApi(SerializerMixin, APIView):
    serializer_class = ProductSerializer
    def post(self, request):
        serializer = ProductSerializer(data=request.data)
        if serializer.is_valid():
            return Response("Valid serializer", status=status.HTTP_201_CREATED)
        return Response(
                            serializer.errors, 
                            status=status.HTTP_400_BAD_REQUEST
                        )

but when i tried this i am getting this error.

Exception Value: 'NotImplementedType' object is not callable

How can i save base64 file in the database using django rest framework

Thameem
  • 3,426
  • 4
  • 26
  • 32

1 Answers1

3

As said in drf-extra-fields docs:

You have to provide your own full implementation of this class. You have to implement file validation in get_file_extension method and set ALLOWED_TYPES list.

You use the default Base64FileField, that's why you receive:

Exception Value: 'NotImplementedType' object is not callable

In this case, you need to extend the default Base64FileField and make your custom field, along with validation method get_file_extension and set ALLOWED_TYPES list as a property.

An example directly from the docs:

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

    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'

Here this is a field for PDF files. Then in your ProductSerializer you can use the new field: file = PDFBase64FileField().

To support more file types, try the filetype library.

phoenix
  • 7,988
  • 6
  • 39
  • 45
wencakisa
  • 5,850
  • 2
  • 15
  • 36
  • file format will be anything like docs, pdf or image . so how can i handle this – Thameem Aug 19 '17 at 09:43
  • 1
    @Thameem At first, set `ALLOWED_TYPES = ('pdf', 'doc', 'docx', 'jpeg', 'jpg', 'png', ...)` and whatever extensions you want. Then, perform validations in your `get_file_extension` method. You already have PDF validation, it's up to you to implement the others. – wencakisa Aug 19 '17 at 09:55
  • @wencaksis could you please point me to validation of jpeg and pdf in a single method. how can i do that. – Thameem Aug 19 '17 at 10:22
  • @Thameem Hope this will help you: https://stackoverflow.com/questions/6640605/detecting-if-a-file-is-an-image-in-python You can implement separate validation for each type and then call them all in `get_file_extension` – wencakisa Aug 19 '17 at 10:26