【问题标题】:AttributeError: 'file' object has no attribute 'open' in Django while assigning a local file to the FileField [duplicate]AttributeError:在将本地文件分配给FileField时,Django中的“文件”对象没有“打开”属性[重复]
【发布时间】:2010-08-17 11:59:55
【问题描述】:

可能重复:
How to assign a local file to the FileField in Django?

我试图将一个文件从我的磁盘分配给 FileField,但是我遇到了这个错误:

AttributeError: 'file' 对象没有属性 'open'

我的python代码:

pdfImage = FileSaver()
myPdfFile = open('mytest.pdf')
pdfImage.myfile.save('new', myPdfFile)

还有我的models.py

class FileSaver(models.Model):

    myfile = models.FileField(upload_to="files/")

    class Meta:
        managed=False

提前感谢您的帮助

【问题讨论】:

  • 这和你一个小时前问的问题本质上不是一样的吗? stackoverflow.com/questions/3501588/…您是否有任何具体原因再次将其作为新问题发布?
  • 不要每隔一个小时重新发布相同的问题。

标签: python django file filefield


【解决方案1】:

http://www.nitinh.com/2009/02/django-example-filefield-and-imagefield/。你需要通过 save 一个 Django 请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-30
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多