decompression - django multiwidget subclass not calling decompress() -


i trying implement multivaluefield ip adress/domain name entries. works expected entering data. problem if want display form bound specific data, ip address/domain name field stays empty. other fields filled desired data. if use normal charfield, data expect. not work custom field. have tracked down fact custom multiwidget not call decompress method.

here field:

class accessipfield(forms.multivaluefield):     """                                                                              custom field access ip                                                       """                                                                               def __init__(self, *args, **kwargs):                                                 self.fields=(                                                                        forms.ipaddressfield(label='ip adress'),                                         forms.charfield(max_length=50,label='domain name')                           )                                                                                self.widget=accessipwidget()                                                     super(accessipfield,self).__init__(self.fields,self.widget, *args, **kwargs)      def compress(self,data_list):                                                        if data_list:                                                                        return " ".join(data_list)  

and here widget:

class accessipwidget(forms.multiwidget):     """      widget display ip adress / domain name pairs     """     def __init__(self,*args,**kwargs):         self.widgets=(forms.textinput(),forms.textinput())         super(accessipwidget,self).__init__(self.widgets,*args,**kwargs)      def decompress(self,value):         print 'decompress called'         if value:             return value.rsplit()         return [none,none]      def format_output(self, rendered_widgets):         return u'\n'.join(rendered_widgets) 

the whole thing called (in larger context) as

self.fields['access_ips'] = accessipfield() 

now can see, put print statement in compress method, , never see statement. also, if rename compress foobar, expect (according django code multiwidget) notimplementederror, not case. suggestions?

i using python 2.6.5, django 1.1 on ubuntu server 10.04.

it turns out problem value_from_datadict() method implemented multiwidget. first of all, allready returned list, why decompress() not called in first place. secondly, allways returen [none,none] list, why bound form stayed empty.

i needed implement own (within accessipwidget class):

def value_from_datadict(self, data, files, name): try:   return data.get(name,none).rsplit() except attributeerror:   return [widget.value_from_datadict(data, files, name + '_%s' % i) i, widget in enumerate(self.widgets)] 

now last line original method did. in order data bound form, needed add data.get(name,none).rsplit().

as far understand, original value_from_datadict method works unbound fields. because changes name of original field name + '_%s', when pressing submit button. in order fill in bound method, datadict needs queried 'name' only.

hm, not shure if there way around this, seems me behaviour should @ least documented somewhere. maybe misunderstood something?


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -