Opened 9 years ago
Closed 9 years ago
#26260 closed New feature (wontfix)
Support of object_list in ModelChoiceField and ModelMultipleChoiceField
Reported by: | Sven R. Kunze | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | tzanke@… | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
We encountered the need of having object_list (list of model instances) instead of a queryset several times when initializing a ModelChoiceField or ModelMultipleChoiceField.
Is there some chance of getting this implemented?
Change History (6)
comment:1 by , 9 years ago
comment:2 by , 9 years ago
I'm probably remembering #23672 (marked as wontfix) which proposes similar functionality for BaseModelFormset
.
comment:3 by , 9 years ago
Cc: | added |
---|
comment:4 by , 9 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
comment:5 by , 9 years ago
Resolution: | needsinfo |
---|---|
Status: | closed → new |
Sure, recreating a queryset from the object list is also our current approach.
We could identify two issues so far:
1) code duplication (already observed)
2) performance
Number 2) worries me most as we observed performance issues recently. So, this only contributes further.
Perhaps it would help when I explain our use-case.
We designed tree widgets for both choice and multiple choice fields. They work quite well when all items can be expressed easily on terms a regular queryset. However, as you can imagine, tree data enables some pretty interesting and complex use-cases. So, the object list is built differently here.
Currently, we cheat by using the ModelChoiceIterator as it exposes the original queryset to the widget.
comment:6 by , 9 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Have you considered a custom field instead? Looking at the code of the fields you mentioned, it doesn't look to me like a dual purpose implementation that allows either a queryset or a list of instances would be simple. As to whether or not such a field should be included in Django itself, I'd suggest to write to the DevelopersMailingList to get feedback on it if you have some code to show. Thanks!
I seem to remember this being discussed before but am not finding anything right away. Is there a problem converting a list of instances to a queryset using something like
queryset=Model.objects.filter(pk__in=[x.pk for x in object_list])
?