@@ -109,6 +109,16 @@ def __init__(self, *args, fields=None, model=None, **kwargs):
109
109
self .widget .widgets .values ()):
110
110
widget .attrs ['placeholder' ] = field .label
111
111
112
+ def prepare_value (self , value ):
113
+ """
114
+ Prepare the field data for the CompositeTypeWidget, which expects data
115
+ as a dict.
116
+ """
117
+ if isinstance (value , CompositeType ):
118
+ return value .__to_dict__ ()
119
+
120
+ return value
121
+
112
122
def validate (self , value ):
113
123
pass
114
124
@@ -155,7 +165,9 @@ def get_bound_field(self, form, field_name):
155
165
156
166
class CompositeTypeWidget (forms .Widget ):
157
167
"""
158
- Takes an ordered dict of widgets to produce a composite form widget
168
+ Takes an ordered dict of widgets to produce a composite form widget. This
169
+ widget knows nothing about CompositeTypes, and works only with dicts for
170
+ initial and output data.
159
171
"""
160
172
template_name = \
161
173
'postgres_composite_types/forms/widgets/composite_type.html'
@@ -170,7 +182,7 @@ def __init__(self, widgets, **kwargs):
170
182
171
183
@property
172
184
def is_hidden (self ):
173
- return all (w .is_hidden for w in self .widgets )
185
+ return all (w .is_hidden for w in self .widgets . values () )
174
186
175
187
def get_context (self , name , value , attrs ):
176
188
context = super ().get_context (name , value , attrs )
@@ -187,12 +199,9 @@ def get_context(self, name, value, attrs):
187
199
if id_ :
188
200
widget_attrs ['id' ] = '%s-%s' % (id_ , subname )
189
201
190
- subwidgets [subname ] = widget .render ('%s-%s' % (name , subname ),
191
- getattr (value , subname , None ),
192
- final_attrs )
193
202
widget_context = widget .get_context (
194
203
'%s-%s' % (name , subname ),
195
- getattr ( value , subname , None ),
204
+ value . get ( subname ),
196
205
widget_attrs )
197
206
subwidgets [subname ] = widget_context ['widget' ]
198
207
0 commit comments