gpt4 book ai didi

python - Django:使用模型中的 FileField 将文件上传到 REST API

转载 作者:太空宇宙 更新时间:2023-11-03 14:28:40 25 4
gpt4 key购买 nike

所以这是上下文。我使用 Django 和 Python3.6,并且我们使用 Django REST Framework 来构建我们的 REST API。我在页面上有一个简单的表单,但没有直接提交。我使用 axios 手动调用我们的 API。

这是按下提交按钮时使用的代码:

let form = new FormData();
form.append('name', this.game.name);
form.append('video', this.game.video);
form.append('icon', this.game.icon);

axios.post("api/games/", form, {headers: {
'Content-Type': 'multipart/form-data',
}})

这里一切都很好。我的 API 接收到正确的数据,我可以轻松创建我的游戏对象。

主要问题是“视频”字段。它在我的 models.py 中注册为 FileField

name = models.CharField(max_length=255, unique=True)
icon = models.ImageField(upload_to=user_directory_path)
video = models.FileField(upload_to=user_directory_path)

当提交类似500ko的MP4视频文件(.mp4)时,它可以正常工作。

但是当我尝试上传一个更大的文件时,例如 5 mo 且格式为 QT (.mov),我从 django 收到错误:

ERROR 2017-11-23 16:59:31,831 exception Internal Server Error: /api/games/
Traceback (most recent call last):
File "myawesomeproject/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "myawesomeproject/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "myawesomeproject/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "myawesomeproject/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "myawesomeproject/lib/python3.6/site-packages/rest_framework/viewsets.py", line 90, in view
return self.dispatch(request, *args, **kwargs)
File "myawesomeproject/lib/python3.6/site-packages/rest_framework/views.py", line 489, in dispatch
response = self.handle_exception(exc)
File "myawesomeproject/lib/python3.6/site-packages/rest_framework/views.py", line 449, in handle_exception
self.raise_uncaught_exception(exc)
File "myawesomeproject/lib/python3.6/site-packages/rest_framework/views.py", line 486, in dispatch
response = handler(request, *args, **kwargs)
File "myawesomeproject/lib/python3.6/site-packages/rest_framework/mixins.py", line 21, in create
self.perform_create(serializer)
File "myawesomeproject/lib/python3.6/site-packages/rest_framework/mixins.py", line 26, in perform_create
serializer.save()
File "myawesomeproject/lib/python3.6/site-packages/rest_framework/serializers.py", line 214, in save
self.instance = self.create(validated_data)
File "myawesomeproject/api/serializers.py", line 114, in create
return super().create(validated_data)
File "myawesomeproject/lib/python3.6/site-packages/rest_framework/serializers.py", line 913, in create
instance = ModelClass.objects.create(**validated_data)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/query.py", line 394, in create
obj.save(force_insert=True, using=self.db)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/base.py", line 808, in save
force_update=force_update, update_fields=update_fields)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/base.py", line 838, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/base.py", line 924, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/base.py", line 963, in _do_insert
using=using, raw=raw)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/query.py", line 1076, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1106, in execute_sql
for sql, params in self.as_sql():
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1059, in as_sql
for obj in self.query.objs
File "myawesomeproject/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1059, in <listcomp>
for obj in self.query.objs
File "myawesomeproject/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 "myawesomeproject/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 "myawesomeproject/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 "myawesomeproject/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 "myawesomeproject/lib/python3.6/site-packages/django/core/files/storage.py", line 54, in save
return self._save(name, content)
File "myawesomeproject/lib/python3.6/site-packages/django/core/files/storage.py", line 338, in _save
file_move_safe(content.temporary_file_path(), full_path)
File "myawesomeproject/lib/python3.6/site-packages/django/core/files/move.py", line 58, in file_move_safe
with open(old_file_name, 'rb') as old_file:
FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/wc/6cny2lg147l55mt4zm92zw9m0000gn/T/tmp6xuujgz2.upload'

我在网上读到,Django 根据文件的大小以不同的方式处理文件,这让我相信它来自于此。

但是除了一些信息之外,我找不到任何解决我的问题的方法。

我在这里做错了什么吗?

最佳答案

我已经解决了我的问题。我犯了一个严重的错误,对此深表歉意。

我在验证步骤中重命名了我的文件。问题是它适用于 <2.5Mb 的图像和视频,所以我不认为它可能来自它。

为了解决这个问题,我删除了验证器中的重命名,只编辑了我在 FileField 的 upload_to 参数中设置的方法。

关于python - Django:使用模型中的 FileField 将文件上传到 REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47460570/

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