Django

Code

Ticket #6314: 6314-2.diff

File 6314-2.diff, 1.9 kB (added by mattmcc, 10 months ago)
  • tests/modeltests/model_forms/models.py

    old new  
    325325>>> print f 
    326326<tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /><br />Use both first and last names.</td></tr> 
    327327 
     328The 'initial' argument to a ModelForm can be a MultiValueDict. 
     329>>> from django.utils.datastructures import MultiValueDict 
     330>>> f = CategoryForm(initial=MultiValueDict({'name': ['V1', 'V2']})) 
     331>>> print f['name'] 
     332<input id="id_name" type="text" name="name" value="V2" maxlength="20" /> 
     333 
    328334>>> art = Article(headline='Test article', slug='test-article', pub_date=datetime.date(1988, 1, 4), writer=w, article='Hello.') 
    329335>>> art.save() 
    330336>>> art.id 
  • django/newforms/models.py

    old new  
    77 
    88from django.utils.translation import ugettext_lazy as _ 
    99from django.utils.encoding import smart_unicode 
    10 from django.utils.datastructures import SortedDict 
     10from django.utils.datastructures import SortedDict, MultiValueDict 
    1111from django.core.exceptions import ImproperlyConfigured 
    1212 
    1313from util import ValidationError, ErrorList 
     
    272272            object_data = model_to_dict(instance, opts.fields, opts.exclude) 
    273273        # if initial was provided, it should override the values from instance 
    274274        if initial is not None: 
    275             object_data.update(initial) 
     275            if isinstance(initial, MultiValueDict): 
     276                for k, v in initial.items(): 
     277                    object_data[k] = v 
     278            else: 
     279                object_data.update(initial) 
    276280        BaseForm.__init__(self, data, files, auto_id, prefix, object_data, error_class, label_suffix) 
    277281 
    278282    def save(self, commit=True):