Ticket #6939: modeladmin_initial_parameters.diff

File modeladmin_initial_parameters.diff, 1.8 KB (added by Matthias Pronk <django@…>, 7 years ago)
  • django/newforms/models.py

     
    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
     
    254254            object_data = model_to_dict(instance, opts.fields, opts.exclude)
    255255        # if initial was provided, it should override the values from instance
    256256        if initial is not None:
     257            # If initial is an instance of MultiValueDict (or QueryDict), "flatten" it
     258            if isinstance(initial, MultiValueDict):
     259                initial = dict(initial.items())
    257260            object_data.update(initial)
    258261        BaseForm.__init__(self, data, files, auto_id, prefix, object_data, error_class, label_suffix)
    259262
  • tests/modeltests/model_forms/models.py

     
    596596<option value="4">Fourth</option>
    597597</select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li>
    598598
     599Use request data to initialize a form:
     600>>> class ArticleForm(ModelForm):
     601...     class Meta:
     602...         model = Article
     603>>> from django.http import QueryDict
     604>>> f = ArticleForm(initial=QueryDict('writer=3'))
     605>>> f.initial
     606{u'writer': u'3'}
     607
    599608# ModelChoiceField ############################################################
    600609
    601610>>> from django.newforms import ModelChoiceField, ModelMultipleChoiceField
Back to Top