Django

Code

Ticket #6314 (closed: wontfix)

Opened 8 months ago

Last modified 7 months ago

ModelForm doesn't support MultiValueDict as initial argument

Reported by: semenov Assigned to: nobody
Milestone: Component: Forms
Version: SVN Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

The following code works fine:

class MyForm(forms.Form):
  foo = forms.CharField()

form = MyForm(initial=request.GET) # can be accessed as http://host/view/?foo=default_value

The following code does not fetch the values from request.GET:

class Foo:
  foo = models.CharField(max_length=255)

class MyForm(forms.ModelForm):
  class Meta:
    model = Foo

form = MyForm(Foo(), initial=request.GET) # does nothing

However, the following hack can be used to fetch the values from request.GET:

class Foo:
  foo = models.CharField(max_length=255)

class MyForm(forms.ModelForm):
  class Meta:
    model = Foo

form = MyForm(Foo(), initial=dict(request.GET.items())) # works as expected

Suggested behaviour: initial argument to ModelForm? should accept MultiValueDict?'s, as the base Form implementation does.

Attachments

6314-1.diff (1.6 kB) - added by mattmcc on 01/09/08 16:42:42.
6314-2.diff (1.9 kB) - added by mattmcc on 01/30/08 17:17:47.

Change History

01/09/08 16:42:13 changed by mattmcc

  • needs_better_patch changed.
  • needs_tests changed.
  • summary changed from ModelForm doesn't accept MultiValueDict as initial argument to ModelForm doesn't support MultiValueDict as initial argument.
  • needs_docs changed.
  • has_patch set to 1.
  • stage changed from Unreviewed to Accepted.

It's not so much that a MultiValueDict isn't accepted, but that its values will be seen as lists, since a dict.update() doesn't know what a MultiValueDict is.

01/09/08 16:42:42 changed by mattmcc

  • attachment 6314-1.diff added.

01/30/08 17:17:47 changed by mattmcc

  • attachment 6314-2.diff added.

01/30/08 19:58:02 changed by SmileyChris

  • stage changed from Accepted to Ready for checkin.

02/02/08 20:32:10 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to wontfix.

No, I don't think this is worth doing. There's no real use case for not just using a normal dictionary for the "initial" parameter. The example given in the ticket report isn't convincing, as GET data is designed to passed via the "data" parameter, not via "initial".

If somebody has a multi-value structure, they can do type the extra characters to convert it to something that is a dictionary proxy fairly easily, so we shouldn't have to add support for this in Django.


Add/Change #6314 (ModelForm doesn't support MultiValueDict as initial argument)




Change Properties
Action