Skip to content

Commit 5774190

Browse files
Merge pull request #445 from WasinTh/drf-extra-fields-base64-integration
Drf extra fields base64 integration
2 parents ea5c3b6 + ba54faa commit 5774190

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

README.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,28 @@ provided out of the box - if you have ``djangorestframework-recursive`` installe
358358

359359
.. |nbsp| unicode:: 0xA0
360360
:trim:
361+
362+
drf-extra-fields
363+
===============================
364+
Integration with `drf-extra-fields <https://github.com/Hipo/drf-extra-fields>`_ has a problem with Base64 fields. The drf-yasg will generate Base64 file or image fields as Readonly and not required. Here is a workaround code for display the Base64 fields correctly.
365+
366+
.. code:: python
367+
368+
class PDFBase64FileField(Base64FileField):
369+
ALLOWED_TYPES = ['pdf']
370+
371+
class Meta:
372+
swagger_schema_fields = {
373+
'type': 'string',
374+
'title': 'File Content',
375+
'description': 'Content of the file base64 encoded',
376+
'read_only': False # <-- FIX
377+
}
378+
379+
def get_file_extension(self, filename, decoded_file):
380+
try:
381+
PyPDF2.PdfFileReader(io.BytesIO(decoded_file))
382+
except PyPDF2.utils.PdfReadError as e:
383+
logger.warning(e)
384+
else:
385+
return 'pdf'

0 commit comments

Comments
 (0)