Джанго: get_or_create() и FileField

Мне очень нравятся методы get_or_create() и update_or_create().

Похоже, они не работают с FielField .

Если я попробую это:

blob, created = Blob.objects.get_or_create(name='foo2', 
                     defaults=dict(content_file='my_content'))

... Я получил:

/home/u/bin/python /home/u/src/test-filefield-get-or-create.py
Traceback (most recent call last):
  File "/home/u/src/test-filefield-get-or-create.py", line 7, in <module>
    print(blob.content_new.read())
  File "/home/u/src/modlink/modlink/models/blob.py", line 65, in _get_content
    self._cached_content = self.content_file.read()
  File "/home/u/lib/python2.7/site-packages/django/core/files/utils.py", line 16, in <lambda>
    read = property(lambda self: self.file.read)
  File "/home/u/lib/python2.7/site-packages/django/db/models/fields/files.py", line 51, in _get_file
    self._file = self.storage.open(self.name, 'rb')
  File "/home/u/lib/python2.7/site-packages/django/core/files/storage.py", line 38, in open
    return self._open(name, mode)
  File "/home/u/lib/python2.7/site-packages/minio_storage/storage.py", line 106, in _open
    "File {} could not be saved: {}".format(name, str(e)), e)
minio_storage.errors.MinIOError: File x could not be saved: NoSuchKey: message: The specified key does not exist.

Моя модель:

class Blob(models.Model):
    content_file = models.FileField(null=True)
    name = models.CharField(max_length=10000, default='')

Обновлять

Если я попробую это:

import django
from django.core.files.base import ContentFile

django.setup()
from modlink.models import Blob


blob = Blob.objects.create(content_file=ContentFile('x'))
print(blob.content_file.read())

Я получаю это:

Traceback (most recent call last):
  File "/home/u/src/test-filefield-get-or-create.py", line 9, in <module>
    print(blob.content_file.read())
  File "/home/u/local/lib/python2.7/site-packages/django/core/files/utils.py", line 16, in <lambda>
    read = property(lambda self: self.file.read)
  File "/home/u/local/lib/python2.7/site-packages/django/db/models/fields/files.py", line 49, in _get_file
    self._require_file()
  File "/home/u/local/lib/python2.7/site-packages/django/db/models/fields/files.py", line 46, in _require_file
    raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)
ValueError: The 'content_file' attribute has no file associated with it.

Просто для записей, я думаю, было бы здорово, если бы вы могли использовать двоичные данные в create(), get_or_create() и update_or_create(), как и другие типы данных. Для этого потребуется «NamelessFileField». Я написал свою идею здесь: https://groups.google.com/forum/#!topic/django-users/7UmFbIg4aE8


person guettli    schedule 29.03.2019    source источник
comment
вы можете опубликовать свой код models.py   -  person rahul.m    schedule 29.03.2019
comment
@c.grey Я обновил вопрос, добавил модель.   -  person guettli    schedule 29.03.2019
comment
ты пробовал content_file=ContentFile('my_content')? docs.djangoproject.com/en /2.1/ссылка/файлы/файл/   -  person Brown Bear    schedule 29.03.2019
comment
@BearBrown Я обновил вопрос. Теперь там вырезано с помощью ContentFile('x')   -  person guettli    schedule 29.03.2019
comment
я думаю, что следующий шаг здесь: stackoverflow.com/a/44756903/8060120   -  person Brown Bear    schedule 29.03.2019