1

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

类 SendMessageSerializer(serializers.ModelSerializer):

class Meta:
     model=ChatMessages

def validate_file(self,attrs,source):
   file=attrs['file']
   messages=attrs['messages']
   if file and messages:
       raise serializers.ValidationError('You can select only one field')
   return  file

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

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

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

4

0 回答 0