def serialize_form(self, form):
"""
Given this job's bound form return the form's data as a session serializable object
The field order is preserved from the original form
"""
data = []
for field_name, field in form.fields.items():
if field_name in form.cleaned_data:
form_value = form.cleaned_data[field_name]
display_value = None
if isinstance(form_value, models.Model):
ctype = ContentType.objects.get_for_model(form_value)
form_value = '{0}{1}.{2}:{3}'.format(
_CONTENT_TYPE_PREFIX,
ctype.app_label,
ctype.model,
form_value.pk
)
elif isinstance(form_value, UploadedFile):
file_name = _fs.get_available_name(form_value.name)
file_path = _fs.path(file_name)
with open(file_path, 'wb+') as destination:
for chunk in form_value.chunks():
destination.write(chunk)
form_value = file_path
display_value = file_name
data.append({
'name': field_name,
'label': force_text(field.label) if field.label else None,
'value': form_value,
'display_value': display_value,
})
return data
评论列表
文章目录