class ImageWithDesiredSize(serializers.Serializer)
image = serializers.ImageField(required=True)
sizes = serializers.ListField(child=serializers.IntegerField())
MergeDict(
<QueryDict: {
u'sizes': [u'200', u'100'],
<MultiValueDict: {u'image': [<InMemoryUploadedFile: abc.png (image/png)>]
}>
)
class FormDataListField(serializers.ListField):
"""
Slight hack around the ListField failing when used inside a multipart/formdata post, for
example when posting an image along with other data
"""
def get_value(self, dictionary):
if html.is_html_input(dictionary):
# don't do this (from base ListField)? it always returns empty
# return html.parse_html_list(dictionary, prefix=self.field_name)
return dictionary.getlist(self.field_name)
return dictionary.get(self.field_name, empty)
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
class ListFieldFormData(serializers.ListField):
def get_value(self, dictionary):
if html.is_html_input(dictionary):
# try out both "name" and "name[]"
field_names = [self.field_name, self.field_name + '[]']
for name in field_names:
if name in dictionary:
return dictionary.getlist(name)
return dictionary.get(self.field_name, empty)
from rest_framework.utils import html
from rest_framework.fields import empty
import logging
logger = logging.getLogger(__name__)
class CustomListField(serializers.ListField):
def get_value(self, dictionary):
if self.field_name not in dictionary:
if getattr(self.root, 'partial', False):
return empty
# We override the default field access in order to support
# lists in HTML forms.
if html.is_html_input(dictionary):
# BEGIN NEW CODE - MARK MIKOFSKI - 2015/10/14
logger.debug('*************** entering get_value() ***************')
logger.debug('dictionary:\n\t%r', dictionary)
logger.debug('field name: %r', self.field_name)
logger.debug('is html input: %r', html.is_html_input(dictionary))
listval = dictionary.getlist(self.field_name)
logger.debug('field name MultiValueDict value list:\n\t%r', listval)
if len(listval) == 1 and isinstance(listval[0], basestring):
logger.debug('val list is a single string:\n\t%r', listval[0])
listval = listval[0] # get single string in value list
if listval.startswith('[') and listval.endswith(']'):
# remove any brackets from string
listval = listval[1:-1]
listval = listval.split(',')
dictionary.setlist(
self.field_name,
[self.child.to_representation(v) for v in listval]
)
listval = dictionary.getlist(self.field_name)
logger.debug('new dict val:\n\t%r', listval)
logger.debug('length of new dict val: %r', len(listval))
logger.debug('*************** leaving get_value() ***************')
# BEGIN NEW CODE - MARK MIKOFSKI - 2015/10/14
val = dictionary.getlist(self.field_name, [])
if len(val) > 0:
# Support QueryDict lists in HTML input.
return val
return html.parse_html_list(dictionary, prefix=self.field_name)
return dictionary.get(self.field_name, empty)
dictionary.setlist(
self.field_name,
[self.child.to_representation(v) for v in listval]
)
dictionary.setlist(self.field_name, self.to_representation(listval))