3

Django 1.11.6

I am trying to save a file through Django shell. Could you have a look at the traceback and tell me what I do wrongly?

Model:

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"))

In the 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:

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'

1 Answer 1

2

The Django File class expects a Python file object, not a path/filename:

f = File(open('/home/…/text_1.pdf', 'rb'))

Django docs: https://docs.djangoproject.com/en/1.11/topics/files/#the-file-object (also see the note on closing opened files)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.