Ticket #26905: 0001-Added-possibility-to-use-any-MultiValueDict-like-obj.patch

File 0001-Added-possibility-to-use-any-MultiValueDict-like-obj.patch, 3.5 KB (added by Marcin Nowak, 8 years ago)

Duck typing proposal for value_from_datadict()

  • django/forms/widgets.py

    From 3064b83919b3cfff004419b88724dd7852e51679 Mon Sep 17 00:00:00 2001
    From: Marcin Nowak <marcin.j.nowak@gmail.com>
    Date: Sun, 17 Jul 2016 15:10:13 +0200
    Subject: [PATCH] Added possibility to use any MultiValueDict-like objects as a
     form data
    
    ---
     django/forms/widgets.py               | 17 ++++++++++-------
     tests/forms_tests/tests/test_forms.py | 19 +++++++++++++++++++
     2 files changed, 29 insertions(+), 7 deletions(-)
    
    diff --git a/django/forms/widgets.py b/django/forms/widgets.py
    index 604a6a4..5c6d6e9 100644
    a b from django.conf import settings  
    1313from django.forms.utils import flatatt, to_current_timezone
    1414from django.templatetags.static import static
    1515from django.utils import datetime_safe, formats, six
    16 from django.utils.datastructures import MultiValueDict
    1716from django.utils.dates import MONTHS
    1817from django.utils.deprecation import (
    1918    RemovedInDjango20Warning, RenameMethodsBase,
    class MultipleHiddenInput(HiddenInput):  
    331330        return mark_safe('\n'.join(inputs))
    332331
    333332    def value_from_datadict(self, data, files, name):
    334         if isinstance(data, MultiValueDict):
    335             return data.getlist(name)
    336         return data.get(name)
     333        try:
     334            getter = data.getlist
     335        except AttributeError:
     336            getter = data.get
     337        return getter(name)
    337338
    338339
    339340class FileInput(Input):
    class SelectMultiple(Select):  
    604605        return mark_safe('\n'.join(output))
    605606
    606607    def value_from_datadict(self, data, files, name):
    607         if isinstance(data, MultiValueDict):
    608             return data.getlist(name)
    609         return data.get(name)
     608        try:
     609            getter = data.getlist
     610        except AttributeError:
     611            getter = data.get
     612        return getter(name)
    610613
    611614
    612615@html_safe
  • tests/forms_tests/tests/test_forms.py

    diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py
    index 2bd49b7..d626150 100644
    a b Java</label></li>  
    855855                widget=CheckboxSelectMultiple,
    856856            )
    857857
     858        class MultiValueDictLike(dict):
     859            def getlist(self, key):
     860                return [self[key]]
     861
    858862        data = {'name': 'Yesterday', 'composers': ['J', 'P']}
    859863        f = SongForm(data)
    860864        self.assertEqual(f.errors, {})
    Java</label></li>  
    867871        f = SongForm(data)
    868872        self.assertEqual(f.errors, {})
    869873
     874        # checking whether MultiValueDictLike.getlist() is called,
     875        # even if composers value is a string
     876        data = MultiValueDictLike(dict(name='Yesterday', composers='J'))
     877        f = SongForm(data)
     878        self.assertEqual(f.errors, {})
     879
    870880    def test_multiple_hidden(self):
     881        class MultiValueDictLike(dict):
     882            def getlist(self, key):
     883                return [self[key]]
     884
    871885        class SongForm(Form):
    872886            name = CharField()
    873887            composers = MultipleChoiceField(
    Java</label></li>  
    904918        self.assertEqual(f.cleaned_data['composers'], ['J', 'P'])
    905919        self.assertEqual(f.cleaned_data['name'], 'Yesterday')
    906920
     921        f = SongForm(MultiValueDictLike(dict(name='Yesterday', composers='J')), auto_id=False)
     922        self.assertEqual(f.errors, {})
     923        self.assertEqual(f.cleaned_data['composers'], ['J'])
     924        self.assertEqual(f.cleaned_data['name'], 'Yesterday')
     925
    907926    def test_escaping(self):
    908927        # Validation errors are HTML-escaped when output as HTML.
    909928        class EscapingForm(Form):
Back to Top