Skip to content

Instantly share code, notes, and snippets.

@zealfire
Last active October 31, 2016 18:03
Show Gist options
  • Save zealfire/f5034410db504296078aec59ebf6a2f8 to your computer and use it in GitHub Desktop.
Save zealfire/f5034410db504296078aec59ebf6a2f8 to your computer and use it in GitHub Desktop.
Code snippet useful for saving image in django model when you know image's path.
def save_image(file_name, new_file_name, directory):
"""
:param file_name(string) old name of image
:param new_file_name(string) new name of image
:param directory(string) name of directory from which image has been fetched
helps to create images
"""
tags_list = ['vd-images']
applicable_languages_list = ['en']
# over here import the django model specific to images
image_object = models.Image()
image_object.tags = tags_list
image_object.alt, image_object.caption, image_object.description = {}, {}, {}
image_object.applicable_languages = applicable_languages_list
image_object.metadata = {'folder_name': directory}
# over here path of image is used
image_object.image = "image/{}".format(new_file_name)
image_object.name = ({'en': file_name})
image_object.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment