Opened 10 years ago

Closed 10 years ago

Last modified 7 years ago

#7457 closed (duplicate)

newforms ModelChoiceField accepts id only

Reported by: vomjom@… Owned by: nobody
Component: Forms Version: master
Severity: Keywords:
Cc: vomjom@… Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Using newforms:

class StepForm(forms.Form):
    procedure = forms.ModelChoiceField(Procedure.objects.all())

foo = StepForm(request.POST)

foo.cleaned_data will look like:

{'procedure': <Procedure: example procedure>}

Which means the cleaned_data has an instance of a Model. That's fine.

But when passing data to a newforms

foo = StepForm({'procedure': some_procedure_instance})

It won't work. You must instead do:

foo = StepForm({'procedure':})

There are a few reasons I think this is a bug:

  1. It would make more sense if cleaned_data and the data argument to a newforms.Form matched up. It'd be nice if newforms.Form allowed both models.Model or an id.
  2. If you're using FormSets from the newforms-admin branch, you can't do this code (this adds more forms, as necessary, to a FormSet):
    old_formset = StepFormSet(request.POST, request.FILES)
    formset = StepFormSet(initial=old_formset.cleaned_data)

Because the cleaned_data contains the instances whereas the newforms.Form requires an id.

Change History (3)

comment:1 Changed 10 years ago by Eric Holscher

milestone: 1.0
Triage Stage: UnreviewedDesign decision needed

comment:2 Changed 10 years ago by Alex Gaynor

Resolution: duplicate
Status: newclosed

Dupe of #7287.

comment:3 Changed 7 years ago by Jacob

milestone: 1.0

Milestone 1.0 deleted

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