Last active
June 12, 2021 12:46
-
-
Save avoiney/5248673 to your computer and use it in GitHub Desktop.
Revisions
-
Alexandre Voiney revised this gist
Jul 1, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ # ... Some field # media = models.FileField(upload_to="path_to_inner_media_folder", blank=True) from django.core.files.base import ContentFile import os # Create an article with the associated model @@ -16,7 +16,7 @@ if fh: # Get the content of the file file_content = ContentFile(fh.read()) # Set the media attribute of the article, but under an other path/filename new_article.media.save(new_file_path, file_content) # Save the article new_article.save() -
Alexandre Voiney renamed this gist
Mar 26, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ # Assuming you have a Article Model like this # class Article(models.Model): # ... Some field # media = models.FileField(upload_to="path_to_inner_media_folder", blank=True) from django.core.files import ContentFile -
Alexandre Voiney revised this gist
Mar 26, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,4 +24,4 @@ if fh: fh.close() if fh.closed: os.remove(unicode(fh.name)) del fh -
Alexandre Voiney revised this gist
Mar 26, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,7 +17,7 @@ if fh: # Get the content of the file file_content = ContentFile(fh.read()) # Set the media attribute of the article with or file, but under an other path/filename new_article.media.save(new_file_path, file_content) # Save the article new_article.save() # Close the file and delete it -
Alexandre Voiney created this gist
Mar 26, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ # Assuming you have a Article Model like this # class Article(models.Model): # ... Some field # media = models.FileField(upload_to="path_to_inner_media_folder", blank=True) from django.core.files import ContentFile import os # Create an article with the associated model new_article = Article() original_file_path = 'path' new_file_path = 'new_path' fh = open(original_file_path, "r") if fh: # Get the content of the file file_content = ContentFile(fh.read()) # Set the media attribute of the article with or file, but under an other path/filename new_article.media.save(new_file_path, content_file) # Save the article new_article.save() # Close the file and delete it fh.close() if fh.closed: os.remove(unicode(fh.name)) del fh