Django

Code

Ticket #5247: patch.diff

File patch.diff, 1.6 kB (added by baumer1122, 11 months ago)

better patch

  • newforms/widgets.py

    old new  
    250250        if value is None: value = [] 
    251251        final_attrs = self.build_attrs(attrs, name=name) 
    252252        output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)] 
    253         str_values = set([force_unicode(v) for v in value]) # Normalize to strings. 
     253        # Normalize to strings 
     254        str_values = set() 
     255        for v in value: 
     256            if hasattr(v, '_get_pk_val'): 
     257                str_values.add(force_unicode(v._get_pk_val())) 
     258            else: 
     259                str_values.add(force_unicode(v)) 
    254260        for option_value, option_label in chain(self.choices, choices): 
    255261            option_value = force_unicode(option_value) 
    256262            selected_html = (option_value in str_values) and ' selected="selected"' or '' 
     
    352358        final_attrs = self.build_attrs(attrs, name=name) 
    353359        output = [u'<ul>'] 
    354360        # Normalize to strings 
    355         str_values = set([force_unicode(v) for v in value]) 
     361        str_values = set() 
     362        for v in value: 
     363            if hasattr(v, '_get_pk_val'): 
     364                str_values.add(force_unicode(v._get_pk_val())) 
     365            else: 
     366                str_values.add(force_unicode(v)) 
    356367        for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): 
    357368            # If an ID attribute was given, add a numeric index as a suffix, 
    358369            # so that the checkboxes don't all have the same ID attribute.