Skip to content

Instantly share code, notes, and snippets.

@ir4y
Created July 25, 2013 03:15
Show Gist options
  • Select an option

  • Save ir4y/6076668 to your computer and use it in GitHub Desktop.

Select an option

Save ir4y/6076668 to your computer and use it in GitHub Desktop.
#Первый вариант использование ContentFile для непосредственной передачи контента в файл
from django.db import models
from django.core.files.base import ContentFile
class Instance(models.Model):
pdf = models.FileField(verbose_name=u'Файл в формате PDF')
instance = Instance()
name = u"somefilename"
instance.pdf.save(name, ContentFile("some pdf value")
instance.save()
#Второй вариант использование временного файла
from django.core.files import File
from django.core.files.temp import NamedTemporaryFile
image_temp = NamedTemporaryFile(delete=True, suffix='.pdf')
image_temp.write("some file content")
image_temp.flush()
instance.pdf = File(image_temp)
instance.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment