Ticket #7195: mergedict.diff

File mergedict.diff, 2.2 KB (added by Daniel Roseman <daniel@…>, 17 years ago)
  • django/newforms/widgets.py

     
    1010import copy
    1111from itertools import chain
    1212
    13 from django.utils.datastructures import MultiValueDict
     13from django.utils.datastructures import MultiValueDict, MergeDict
    1414from django.utils.html import escape, conditional_escape
    1515from django.utils.translation import ugettext
    1616from django.utils.encoding import StrAndUnicode, force_unicode
     
    128128            for v in value]))
    129129
    130130    def value_from_datadict(self, data, files, name):
    131         if isinstance(data, MultiValueDict):
     131        if isinstance(data, MultiValueDict) or isinstance(data, MergeDict):
    132132            return data.getlist(name)
    133133        return data.get(name, None)
    134134
     
    263263        return mark_safe(u'\n'.join(output))
    264264
    265265    def value_from_datadict(self, data, files, name):
    266         if isinstance(data, MultiValueDict):
     266        if isinstance(data, MultiValueDict) or isinstance(data, MergeDict):
    267267            return data.getlist(name)
    268268        return data.get(name, None)
    269269
  • tests/regressiontests/forms/forms.py

     
    539539<li><label><input type="checkbox" name="composers" value="P" id="composers_id_1" /> Paul McCartney</label></li>
    540540</ul>
    541541
    542 Data for a MultipleChoiceField should be a list. QueryDict and MultiValueDict
    543 conveniently work with this.
     542Data for a MultipleChoiceField should be a list. QueryDict, MultiValueDict and
     543MergeDict conveniently work with this.
    544544>>> data = {'name': 'Yesterday', 'composers': ['J', 'P']}
    545545>>> f = SongForm(data)
    546546>>> f.errors
     
    555555>>> f = SongForm(data)
    556556>>> f.errors
    557557{}
     558>>> from django.utils.datastructures import MergeDict
     559>>> data = MergeDict(MultiValueDict(dict(name=['Yesterday'], composers=['J', 'P'])))
     560>>> f = SongForm(data)
     561>>> f.errors
     562{}
    558563
    559564The MultipleHiddenInput widget renders multiple values as hidden fields.
    560565>>> class SongFormHidden(Form):
Back to Top