Django

Code

Changeset 4196

Show
Ignore:
Timestamp:
12/13/06 00:26:04 (2 years ago)
Author:
adrian
Message:

Fixed #3114 -- newforms MultipleChoiceField? now handles MultiValueDicts? properly. Thanks for the patch, Honza Král

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/newforms/forms.py

    r4194 r4196  
    33""" 
    44 
    5 from django.utils.datastructures import SortedDict 
     5from django.utils.datastructures import SortedDict, MultiValueDict 
    66from django.utils.html import escape 
    77from fields import Field 
     
    222222    def _data(self): 
    223223        "Returns the data for this BoundField, or None if it wasn't given." 
     224        if self.field.widget.requires_data_list and isinstance(self.form.data, MultiValueDict): 
     225            return self.form.data.getlist(self.name) 
    224226        return self.form.data.get(self.name, None) 
    225227    data = property(_data) 
  • django/trunk/django/newforms/widgets.py

    r4163 r4196  
    1010 
    1111from util import StrAndUnicode, smart_unicode 
     12from django.utils.datastructures import MultiValueDict 
    1213from django.utils.html import escape 
    1314from itertools import chain 
     
    6566    """ 
    6667    input_type = None # Subclasses must define this. 
     68 
    6769    def render(self, name, value, attrs=None): 
    6870        if value is None: value = '' 
     
    147149        return u'\n'.join(output) 
    148150 
     151    def value_from_datadict(self, data, name): 
     152        if isinstance(data, MultiValueDict): 
     153            return data.getlist(name) 
     154        return data.get(name, None) 
     155 
    149156class RadioInput(StrAndUnicode): 
    150157    "An object used by RadioFieldRenderer that represents a single <input type='radio'>." 
  • django/trunk/tests/regressiontests/forms/tests.py

    r4194 r4196  
    16721672<li><label><input checked="checked" type="checkbox" name="composers" value="P" /> Paul McCartney</label></li> 
    16731673</ul> 
     1674 
     1675Data for a MultipleChoiceField should be a list. QueryDict and MultiValueDict 
     1676conveniently work with this. 
     1677>>> data = {'name': 'Yesterday', 'composers': ['J', 'P']} 
     1678>>> f = SongForm(data) 
     1679>>> f.errors 
     1680{} 
     1681>>> from django.http import QueryDict 
     1682>>> data = QueryDict('name=Yesterday&composers=J&composers=P') 
     1683>>> f = SongForm(data) 
     1684>>> f.errors 
     1685{} 
     1686>>> from django.utils.datastructures import MultiValueDict 
     1687>>> data = MultiValueDict(dict(name='Yesterday', composers=['J', 'P'])) 
     1688>>> f = SongForm(data) 
     1689>>> f.errors 
     1690{} 
    16741691 
    16751692When using CheckboxSelectMultiple, the framework expects a list of input and