Django 1.11.6
我正在尝试通过Django shell保存一个文件。您能否查看追溯信息并告诉我我的错误在哪里?
模型:
class ItemFile(ChecksumMixin,
models.Model):
item = models.ForeignKey(Item,
on_delete=models.PROTECT,
verbose_name=_("item"))
file = models.FileField(blank=False,
max_length=255,
upload_to=get_item_path,
verbose_name=_("file"))
在shell中:
from django.core.files import File
f = File('/home/michael/PycharmProjects/photoarchive_4/general/static/test/text_1.pdf', 'rb')
i = Item.objects.get(pk=1)
ItemFile.objects.create(item=i, file=f)
跟踪信息:
Traceback (most recent call last):
File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1059, in <listcomp>
for obj in self.query.objs
File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1058, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1008, in pre_save_val
return field.pre_save(obj, add=True)
File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/db/models/fields/files.py", line 296, in pre_save
file.save(file.name, file.file, save=False)
File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/db/models/fields/files.py", line 94, in save
self.name = self.storage.save(name, content, max_length=self.field.max_length)
File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/core/files/storage.py", line 54, in save
return self._save(name, content)
File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/core/files/storage.py", line 351, in _save
for chunk in content.chunks():
File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/core/files/base.py", line 81, in chunks
data = self.read(chunk_size)
File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/core/files/utils.py", line 16, in <lambda>
read = property(lambda self: self.file.read)
AttributeError: 'str' object has no attribute 'read'