Opened 16 years ago

Closed 16 years ago

Last modified 13 years ago

#7457 closed (duplicate)

newforms ModelChoiceField accepts id only

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

Description

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': some_procedure_instance.id})

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 by Eric Holscher, 16 years ago

milestone: 1.0
Triage Stage: UnreviewedDesign decision needed

comment:2 by Alex Gaynor, 16 years ago

Resolution: duplicate
Status: newclosed

Dupe of #7287.

comment:3 by Jacob, 13 years ago

milestone: 1.0

Milestone 1.0 deleted

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