Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在序列化程序Django rest框架中处理文件?_Python_Django_Django Rest Framework_Django Serializer - Fatal编程技术网

Python 在序列化程序Django rest框架中处理文件?

Python 在序列化程序Django rest框架中处理文件?,python,django,django-rest-framework,django-serializer,Python,Django,Django Rest Framework,Django Serializer,我在serializer.py中有一个文件字段,需要为文件字段编写自定义验证。我编写的代码如下: 类SendMessageSerializer(serializers.ModelSerializer): 调用此函数时,显示以下错误: TypeError at /chat-message/send/ string indices must be integers Request Method: POST Request URL: http://127.0.0.1:8001/chat-mess

我在serializer.py中有一个文件字段,需要为文件字段编写自定义验证。我编写的代码如下:

类SendMessageSerializer(serializers.ModelSerializer):

调用此函数时,显示以下错误:

TypeError at /chat-message/send/
string indices must be integers
Request Method: POST
Request URL:    http://127.0.0.1:8001/chat-message/send/
Django Version: 1.6
Exception Type: TypeError
Exception Value:    
string indices must be integers
Exception Location: /home/something/projects/something/apps/chats/serializer.py in validate_file, line 13
Python Executable:  /home/something/Virtualenv/something/bin/python
Python Version: 2.7.6
Python Path:    
['/home/ntech/projects/something',
 '/home/ntech/Virtualenv/something/lib/python2.7',
 '/home/ntech/Virtualenv/something/lib/python2.7/plat-i386-linux-gnu',
 '/home/ntech/Virtualenv/something/lib/python2.7/lib-tk',
 '/home/ntech/Virtualenv/something/lib/python2.7/lib-old',
 '/home/ntech/Virtualenv/something/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-i386-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/ntech/Virtualenv/something/local/lib/python2.7/site-packages',
 '/home/ntech/Virtualenv/something/lib/python2.7/site-packages']
Server time:    Fri, 2 Jan 2015 09:33:35 +0000

我需要检查字段是否为文件,如何检查,是否有获取文件的替代解决方案

请向我们展示您的序列化程序的所有代码。@zymud我已经更新了。这是序列化程序中的代码。谢谢,请告诉我您使用的django rest框架的版本是什么?@zymud我使用的djangorestframework==2.4.4。目前我只看到一个错误-方法
验证_文件
必须返回
attrs
(不是
文件
)。但我不认为这会导致你的异常。请在异常之前向我显示
attrs
变量的值(您可以使用断点或仅打印它)。
TypeError at /chat-message/send/
string indices must be integers
Request Method: POST
Request URL:    http://127.0.0.1:8001/chat-message/send/
Django Version: 1.6
Exception Type: TypeError
Exception Value:    
string indices must be integers
Exception Location: /home/something/projects/something/apps/chats/serializer.py in validate_file, line 13
Python Executable:  /home/something/Virtualenv/something/bin/python
Python Version: 2.7.6
Python Path:    
['/home/ntech/projects/something',
 '/home/ntech/Virtualenv/something/lib/python2.7',
 '/home/ntech/Virtualenv/something/lib/python2.7/plat-i386-linux-gnu',
 '/home/ntech/Virtualenv/something/lib/python2.7/lib-tk',
 '/home/ntech/Virtualenv/something/lib/python2.7/lib-old',
 '/home/ntech/Virtualenv/something/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-i386-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/ntech/Virtualenv/something/local/lib/python2.7/site-packages',
 '/home/ntech/Virtualenv/something/lib/python2.7/site-packages']
Server time:    Fri, 2 Jan 2015 09:33:35 +0000