Ticket #338: django-select-prepopulate.patch

File django-select-prepopulate.patch, 2.0 KB (added by robert@…, 13 years ago)

Incomplete fix that is not specific to generic views.

  • django/core/formfields.py

     
    114114    def __getitem__(self, key):
    115115        for field in self.manipulator.fields:
    116116            if field.field_name == key:
    117                 if hasattr(field, 'requires_data_list') and hasattr(self.data, 'getlist'):
    118                     data = self.data.getlist(field.field_name)
    119                 else:
    120                     data = self.data.get(field.field_name, None)
    121                 if data is None:
    122                     data = ''
    123                 return FormFieldWrapper(field, data, self.error_dict.get(field.field_name, []))
     117               
     118                data = field.extract_data(self.data)
     119               
     120                return FormFieldWrapper(field, data, self.error_dict.get(field.field_name, []))
    124121        raise KeyError
    125122
    126123    def has_errors(self):
     
    137134        return str(self.formfield.render(self.data))
    138135
    139136    def __repr__(self):
    140         return '<FormFieldWrapper for "%s">' % self.formfield.field_name
     137        return '<FormFieldWrapper for "%s": %s >' % self.formfield.field_name, repr(self.data)
    141138
    142139    def field_list(self):
    143140        """
     
    209206    def render(self, data):
    210207        raise NotImplementedError
    211208
     209    def get_member_name(self):
     210        return self.field_name
     211
     212    def extract_data(self, data_dict):
     213        if hasattr(self, 'requires_data_list') and hasattr(data_dict, 'getlist'):
     214            data = data_dict.getlist(self.get_member_name())
     215        else:
     216            data = data_dict.get(self.get_member_name(), None)
     217        if data is None:
     218            data = ''
     219       
     220        return data
     221
    212222####################
    213223# GENERIC WIDGETS  #
    214224####################
     
    319329        output.append('  </select>')
    320330        return '\n'.join(output)
    321331
     332    def get_member_name(self):
     333        return "%s_id" % self.field_name
     334
    322335    def isValidChoice(self, data, form):
    323336        str_data = str(data)
    324337        str_choices = [str(item[0]) for item in self.choices]
Back to Top