Code

Opened 6 years ago

Closed 6 years ago

Last modified 3 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:

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.

Attachments (0)

Change History (3)

comment:1 Changed 6 years ago by ericholscher

  • milestone set to 1.0
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 Changed 6 years ago by Alex

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

Dupe of #7287.

comment:3 Changed 3 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.