Opened 7 years ago

Closed 7 years ago

#6314 closed (wontfix)

ModelForm doesn't support MultiValueDict as initial argument

Reported by: 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 mattmcc 7 years ago.
6314-2.diff (1.9 KB) - added by mattmcc 7 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 Changed 7 years ago by mattmcc

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from ModelForm doesn't accept MultiValueDict as initial argument to ModelForm doesn't support MultiValueDict as initial argument
  • Triage 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.

Changed 7 years ago by mattmcc

Changed 7 years ago by mattmcc

comment:2 Changed 7 years ago by SmileyChris

  • Triage Stage changed from Accepted to Ready for checkin

comment:3 Changed 7 years ago by mtredinnick

  • Resolution set to wontfix
  • Status changed from new to closed

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