filefield

copy file from one model to another

流过昼夜 提交于 2019-11-28 07:31:56
I have 2 simple models: class UploadImage(models.Model): Image = models.ImageField(upload_to="temp/") class RealImage(models.Model): Image = models.ImageField(upload_to="real/") And one form class RealImageForm(ModelForm): class Meta: model = RealImage I need to save file from UploadImage into RealImage. How could i do this. Below code doesn't work realform.Image=UploadImage.objects.get(id=image_id).Image realform.save() Tnx for help. Inspired by Gerard's solution I came up with the following code: from django.core.files.base import ContentFile #... class Example(models.Model): file = models

How to assign a local file to the FileField in Django?

青春壹個敷衍的年華 提交于 2019-11-27 20:27:46
I was trying to assign a file from my disk to the FileField, but I have this error: AttributeError: 'str' object has no attribute 'open' My python code: pdfImage = FileSaver() pdfImage.myfile.save('new', open('mytest.pdf').read()) and my models.py class FileSaver(models.Model): myfile = models.FileField(upload_to="files/") class Meta: managed=False Thank you in advance for your help tux21b Django uses it's own file type (with a sightly enhanced functionality). Anyway Django's file type works like a decorator , so you can simply wrap it around existing file objects to meet the needs of the

copy file from one model to another

大兔子大兔子 提交于 2019-11-27 01:54:32
问题 I have 2 simple models: class UploadImage(models.Model): Image = models.ImageField(upload_to="temp/") class RealImage(models.Model): Image = models.ImageField(upload_to="real/") And one form class RealImageForm(ModelForm): class Meta: model = RealImage I need to save file from UploadImage into RealImage. How could i do this. Below code doesn't work realform.Image=UploadImage.objects.get(id=image_id).Image realform.save() Tnx for help. 回答1: Inspired by Gerard's solution I came up with the

how to download a filefield file in django view

强颜欢笑 提交于 2019-11-26 21:37:57
问题 I have filefield in my django model: file=models.FileField(verbose_name=u'附件',upload_to='attachment/%Y/%m/%d',max_length=480) This file will display in the web page with link "http://test.com.cn/home/projects/89/attachment/2012/02/24/sscsx.txt" What I want to do is when user click the file link, it will download the file automatically; Can anyone tell me how to do this in the view? Thanks in advance! 回答1: You can try the following code, assuming that object_name is an object of that model:

How to assign a local file to the FileField in Django?

旧街凉风 提交于 2019-11-26 20:21:01
问题 I was trying to assign a file from my disk to the FileField, but I have this error: AttributeError: 'str' object has no attribute 'open' My python code: pdfImage = FileSaver() pdfImage.myfile.save('new', open('mytest.pdf').read()) and my models.py class FileSaver(models.Model): myfile = models.FileField(upload_to="files/") class Meta: managed=False Thank you in advance for your help 回答1: Django uses it's own file type (with a sightly enhanced functionality). Anyway Django's file type works