serialization - drf Base64ImageField not accepting None value -
here serializer as:
class questionserializer(serializers.modelserializer): choices = choiceserializer(many=true) image = base64imagefield(required=false) class meta: model = question fields = ('id', 'detail', 'image', 'true_false_answer', 'type', 'choices')
base64imagefield described follow:
from django.core.files.base import contentfile rest_framework import serializers rest_framework.fields import skipfield class base64fieldmixin(object): def _decode(self, data): if data.get('filearray'): data = data.get('dataurl') else: data = none if isinstance(data, str) , data.startswith('data:'): # base64 encoded file - decode format, datastr = data.split(';base64,') # format ~= data:image/x, ext = format.split('/')[-1] # guess file extension data = contentfile( base64.b64decode(datastr), name='{}.{}'.format(uuid.uuid4(), ext) ) elif isinstance(data, str) , data.startswith('http'): raise skipfield() return data def to_internal_value(self, data): data = self._decode(data) return super(base64fieldmixin, self).to_internal_value(data) class base64imagefield(base64fieldmixin, serializers.imagefield): pass
when pass image field with:
{ 'filearray' = [] }
i errors this:
image: ["the submitted data not file. check encoding type on form."]
but works when image data is:
{ 'filearray': [<list of file information>], 'baseurl: '<base64 decoded string>' }
what have misunderstood here? :)
Comments
Post a Comment