def to_representation(self, obj):
if obj is None:
return \
super(SubmissionStatsInstanceSerializer, self).to_representation(obj)
request = self.context.get('request')
field = request.query_params.get('group')
name = request.query_params.get('name', field)
if field is None:
raise exceptions.ParseError(_(u"Expecting `group` and `name`"
u" query parameters."))
try:
data = get_form_submissions_grouped_by_field(
obj, field, name)
except ValueError as e:
raise exceptions.ParseError(detail=e.message)
else:
if data:
dd = obj.data_dictionary()
element = dd.get_survey_element(field)
if element and element.type in SELECT_FIELDS:
for record in data:
label = dd.get_choice_label(element, record[name])
record[name] = label
return data
评论列表
文章目录