Skip to content

Commit b99be64

Browse files
Merge pull request #203 from xjodoin/patch-1
Use built-in 'open' method for file object
2 parents b772fa4 + a6c248d commit b99be64

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

drf_extra_fields/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def to_representation(self, file):
111111
return ""
112112

113113
try:
114-
with open(file.path, "rb") as f:
114+
with file.open() as f:
115115
return base64.b64encode(f.read()).decode()
116116
except Exception:
117117
raise OSError("Error encoding file")

tests/test_fields.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class ImageFieldFile:
4444
def __init__(self, path):
4545
self.path = path
4646

47+
def open(self):
48+
return open(self.path, "rb")
49+
4750
def __init__(self, image_path):
4851
self.image = self.ImageFieldFile(path=image_path)
4952

@@ -53,6 +56,9 @@ class FieldFile:
5356
def __init__(self, path):
5457
self.path = path
5558

59+
def open(self):
60+
return open(self.path, "rb")
61+
5662
def __init__(self, file_path):
5763
self.file = self.FieldFile(path=file_path)
5864

0 commit comments

Comments
 (0)