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