Get default form data

Given an unbound form, determine what data would be generated from POSTing the form unchanged. The goal is to end up with a dict such that, passed into another form constructor as its data kwarg, form.changed_data == [].

defget_default_form_data(form):""" Given an unbound form, determine what data would be generated from POSTing the form unchanged. """defvalue_from_widget(initial_value,widget):value=initial_valueifvalue!='':ifhasattr(widget,'_format_value'):value=widget._format_value(value)value=force_unicode(value)returnvaluedata={}forname,fieldinform.fields.items():ifisinstance(field.widget,forms.MultiWidget):value=field.widget.decompress(form.initial[name])fori,widgetinenumerate(field.widget.widgets):try:widget_value=value[i]exceptIndexError:widget_value=Nonedata['%s_%s'%(name,i)]=value_from_widget(widget_value,widget)else:data[name]=value_from_widget(form.initial[name],field.widget)ifform.prefix:returndict([('%s-%s'%(form.prefix,k),v)fork,vindata.items()])else:returndata