#7457 closed (duplicate)
newforms ModelChoiceField accepts id only
Reported by: | 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:
- 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.
- 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 , 16 years ago
milestone: | → 1.0 |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
comment:2 by , 16 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
Dupe of #7287.