#33106: ModelMultipleChoiceField clean() method calls prepare_value instead of
to_python
-----------------------------+--------------------------------------
Reporter: Adam McKay | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: 3.2
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by Carlton Gibson):
* status: new => closed
* type: Bug => New feature
* resolution: => wontfix
Comment:
Hi Adam — thanks for the report. Interesting.
This kind of mapping has never really been supported, so I'd say this is a
new feature rather than a bug. That's probably a small distinction but it
does point to a way forward, which is that it would be good to see the
required implementation out of Django before deciding whether to make a
change here.
> However the clean() method of ModelMultipleChoiceField calls value =
self.prepare_value(value) which is causing issues for my use case…
Your test doesn't seem right. If I comment out the offending line, the
test still fails (`43 is not one of the available choices.` rather than
`85 is not one of the available choices.` — so the `42` is not being
applied the extra time, but neither is it being removed in `to_python`)
but now we get the additional failure from #26970
(4e861682904744b0ea3ead8552513c6f1a826c5a).
I use hashids myself at times. I'm inclined to think addressing the
mapping at the form `Field` level is something of a no-man's land. I'd
either push it towards the model field, which is what
[https://github.com/nshafer/django-hashid-field django-hashid-field] does
— the `choice` then just working — or (which is where I'd look since I
don't tend to use the custom model field) push the mapping to a custom
[https://docs.djangoproject.com/en/3.2/ref/forms/fields/#modelchoiceiterator
ModelChoiceIterator/ModelChoiceIteratorValue pair] for generating the HTML
and a custom widget to map the value back in `value_from_datadict()`, thus
keeping the mapping at the periphery. Otherwise a bit more work on
`ModelMultipleChoiceField` subclass looks needed. (Likely Nathan on
django-hashid-field would be able to guide you more.)
--
Ticket URL: <https://code.djangoproject.com/ticket/33106#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.883121b109019d8cf640abeb0923a4ea%40djangoproject.com.