Opened 9 years ago

Closed 9 years ago

#6314 closed (wontfix)

ModelForm doesn't support MultiValueDict as initial argument

Reported by: Ilya Semenov Owned by: nobody
Component: Forms Version: master
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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 (2)

6314-1.diff (1.6 KB) - added by Matt McClanahan 9 years ago.
6314-2.diff (1.9 KB) - added by Matt McClanahan 9 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 Changed 9 years ago by Matt McClanahan

Has patch: set
Summary: ModelForm doesn't accept MultiValueDict as initial argumentModelForm doesn't support MultiValueDict as initial argument
Triage Stage: UnreviewedAccepted

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.

Changed 9 years ago by Matt McClanahan

Attachment: 6314-1.diff added

Changed 9 years ago by Matt McClanahan

Attachment: 6314-2.diff added

comment:2 Changed 9 years ago by Chris Beaven

Triage Stage: AcceptedReady for checkin

comment:3 Changed 9 years ago by Malcolm Tredinnick

Resolution: wontfix
Status: newclosed

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.

Note: See TracTickets for help on using tickets.
Back to Top