Skip to content

Instantly share code, notes, and snippets.

@avoiney
Last active June 12, 2021 12:46
Show Gist options
  • Save avoiney/5248673 to your computer and use it in GitHub Desktop.
Save avoiney/5248673 to your computer and use it in GitHub Desktop.

Revisions

  1. Alexandre Voiney revised this gist Jul 1, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions FileField_manual_initialisation.py
    Original 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 import ContentFile
    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 with or file, but under an other path/filename
    # 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()
  2. Alexandre Voiney renamed this gist Mar 26, 2013. 1 changed file with 1 addition and 1 deletion.
    Original 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
    # ... Some field
    # media = models.FileField(upload_to="path_to_inner_media_folder", blank=True)

    from django.core.files import ContentFile
  3. Alexandre Voiney revised this gist Mar 26, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion FileField manual initialisation
    Original 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
    del fh
  4. Alexandre Voiney revised this gist Mar 26, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion FileField manual initialisation
    Original 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, content_file)
    new_article.media.save(new_file_path, file_content)
    # Save the article
    new_article.save()
    # Close the file and delete it
  5. Alexandre Voiney created this gist Mar 26, 2013.
    27 changes: 27 additions & 0 deletions FileField manual initialisation
    Original 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