Skip to content

Instantly share code, notes, and snippets.

@maddrum
Last active February 13, 2024 08:27
Show Gist options
  • Save maddrum/ce60447f404a103d0c44b213ecc6fd71 to your computer and use it in GitHub Desktop.
Save maddrum/ce60447f404a103d0c44b213ecc6fd71 to your computer and use it in GitHub Desktop.
Django manually assign file to model
def test_picture_path_is_correct(self):
obj = self.create_obj()
abs_path = os.path.join(settings.TEST_MEDIA_ASSETS_FOLDER, 'profile_picture_1280.png')
obj.picture.save('filename.png', ImageFile(open(abs_path, 'rb'))) # this line reads image and store it as binary
obj.save()
self.assertTrue(os.path.isfile(obj.picture.path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment