gpt4 book ai didi

python - Django 上传文件验证导致 "InMemoryUploadedFile is not supported"

转载 作者:行者123 更新时间:2023-12-05 05:41:21 26 4
gpt4 key购买 nike

我有一个模板,用户可以在其中选择并从他们的计算机上传文件。该文件最终会上传到 S3,但首先会使用自定义验证来验证文件的某些内容。为了检查内容,脚本读取 forms.py 中文件的行:

from io import TextIOWrapper

class UploadForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ('myfilefield',)

def clean_myfilefield(self):
file = self.cleaned_data['myfilefield']
read_file = TextIOWrapper(self.cleaned_data.get('myfilefield'), encoding='ASCII')
headerCounter = 0
for line in read_file:
if ">" in line:
headerCounter += 1
if headerCounter > 1:
error = "Custom error message 1"
raise ValidationError(error)
if headerCounter == 0:
error = "Custom error message 2"
raise ValidationError(error)
return file

验证消息正确显示,但如果文件通过验证,我会收到此错误消息(假设 myfile.txt 是上传的文件):

Input myfile.txt of type: <class 'django.core.files.uploadedfile.InMemoryUploadedFile'> is not supported.

如果我省略 TextIOWrapper 部分和错误检查,验证和上传工作正常。所以我猜想读取文件会将其转换为 InMemoryUploadedFile,这与上传文件不兼容?我尝试寻找将 InMemoryUploadedFile“转换”回“常规”(?) 文件的解决方案,但没有任何效果。

其他相关代码:

views.py

@login_required
def detail_view(request, id):
current_object = MyModel.objects.get(id=id)
context = {}
context["data"] = current_object

if request.method == 'POST':
form = UploadForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return render(request, 'this_page.html', context)
else:
return render(request, 'this_page.html', {"form" : form})
else:
context["form"] = UploadForm()
return render(request, 'this_page.html', context)

models.py(仅相关内容)

class MyModel(models.Model):
myfilefield = models.FileField(upload_to="media/stuff", blank=True, null=True)

使用 Django 3.2

完整回溯:

Environment:


Request Method: POST
Request URL: xxxx

Django Version: 3.2.13
Python Version: 3.9.12
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'storages',
'myapp',
'crispy_forms']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/xxxx/views.py", line 111, in detail_view
form.save()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/forms/models.py", line 468, in save
self.instance.save()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/models/base.py", line 739, in save
self.save_base(using=using, force_insert=force_insert,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/models/base.py", line 776, in save_base
updated = self._save_table(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/models/base.py", line 881, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/models/base.py", line 919, in _do_insert
return manager._insert(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/models/query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1415, in execute_sql
for sql, params in self.as_sql():
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1358, in as_sql
value_rows = [
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1359, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1359, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1310, in pre_save_val
return field.pre_save(obj, add=True)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/models/fields/files.py", line 302, in pre_save
file.save(file.name, file.file, save=False)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/models/fields/files.py", line 89, in save
self.name = self.storage.save(name, content, max_length=self.field.max_length)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/files/storage.py", line 54, in save
name = self._save(name, content)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/storages/backends/s3boto3.py", line 459, in _save
obj.upload_fileobj(content, ExtraArgs=params)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/boto3/s3/inject.py", line 725, in object_upload_fileobj
return self.meta.client.upload_fileobj(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/boto3/s3/inject.py", line 636, in upload_fileobj
return future.result()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/s3transfer/futures.py", line 103, in result
return self._coordinator.result()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/s3transfer/futures.py", line 266, in result
raise self._exception
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/s3transfer/tasks.py", line 269, in _main
self._submit(transfer_future=transfer_future, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/s3transfer/upload.py", line 579, in _submit
upload_input_manager = self._get_upload_input_manager_cls(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/s3transfer/upload.py", line 546, in _get_upload_input_manager_cls
raise RuntimeError(

Exception Type: RuntimeError at /xxxx/12
Exception Value: Input myfile.txt of type: <class 'django.core.files.uploadedfile.InMemoryUploadedFile'> is not supported.

最佳答案

您必须向TextIOWrapper 提供文件对象 .

InMemoryUploadedFile是文件对象的包装器。您可以使用 file 属性访问文件对象。

io.TextIOWrapper 对象在垃圾回收时关闭包装文件(例如 __del__())。我们可以detach你的 TextIOWrapper() 对象。

from io import TextIOWrapper

class UploadForm(forms.ModelForm):
...

def clean_myfilefield(self):
my_file = self.cleaned_data['myfilefield']
HERE ⬇️
read_file = TextIOWrapper(my_file.file, encoding='ASCII')

...
# Here use detach
read_file.detach()

关于python - Django 上传文件验证导致 "InMemoryUploadedFile is not supported",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72285087/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com