我有一个以下序列化器:
class QuestionSerializer(serializers.ModelSerializer):
choices = ChoiceSerializer(many=True)
image = Base64ImageField(required=False)
def validate_image(self, value):
import ipdb
ipdb.set_trace()
# if value.get('fileArray', None):
# if value.get('dataURL', None):
# return value.get('dataURL')
# else:
# raise serializers.ValidationError("No file data present")
# else:
# return None
class Meta:
model = Question
fields = ('id', 'detail', 'image', 'true_false_answer', 'type', 'choices')
然后在 View 中:
serializer = QuestionSerializer(data=data)
serializer.is_valid()
这给了我错误 和:
serializer.errors
给出:
{'image': ['The submitted data was not a file. Check the encoding type on the form.']}
我发现 validate_image 从未被调用。 可能是什么问题?
最佳答案
如果默认 DRF 验证通过,What I found is validate_image is never called. What might be the problem?
validate_image
将被调用。
当序列化器已经知道该字段无效时,验证图像字段是没有意义的。
关于django - drf : custom field validation function not being called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43228274/