Django:使用模型中的 FileField 将文件上传到 REST API
Django: Uploading a file to a REST API with a FileField in a model
这是上下文。我将 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 收到正确的数据,我可以轻松创建我的游戏对象。
主要问题是 'video' 字段。它在我的 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 参数中设置的方法。
这是上下文。我将 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 收到正确的数据,我可以轻松创建我的游戏对象。
主要问题是 'video' 字段。它在我的 models.py
中注册为 FileFieldname = 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 参数中设置的方法。