Opened 18 years ago
Closed 18 years ago
#4787 closed (fixed)
Updating queryset on a ModelChoiceField, ModelMultipleChoiceField
| Reported by: | Owned by: | eads | |
|---|---|---|---|
| Component: | Forms | Version: | dev |
| Severity: | Keywords: | ModelChoiceField ModelMultipleChoiceField Queryset | |
| Cc: | michal@… | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | yes | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
The problem is that updating the queryset for a forms.ModelChoiceField in a form doesn't change widget choices because they are set at ModelChoiceField __init__ time.
This code works (in __init__ of my form):
MyForm.base_fields['activity'].queryset = Activity.objects.filter(user__id=2)
MyForm.base_fields['activity'].widget.choices = MyForm.base_fields['activity'].choices
So I propose the following patch in newforms/models.py
(I've never used "property" before)
class ModelChoiceField(ChoiceField):
.....
def _set_queryset(self, value):
self._queryset = value
self.widget.choices = self.choices
def _get_queryset(self):
return self._queryset
queryset = property(_get_queryset, _set_queryset)
Attachments (3)
Change History (14)
comment:1 by , 18 years ago
| Needs tests: | set |
|---|---|
| Patch needs improvement: | set |
| Triage Stage: | Unreviewed → Accepted |
comment:3 by , 18 years ago
| Keywords: | ModelMultipleChoiceField added |
|---|---|
| Summary: | Updating queryset on a ModelChoiceField → Updating queryset on a ModelChoiceField, ModelMultipleChoiceField |
ModelMultipleChoiceField has the same problem
comment:4 by , 18 years ago
| Cc: | added |
|---|
comment:5 by , 18 years ago
| Owner: | changed from to |
|---|
comment:6 by , 18 years ago
| Patch needs improvement: | unset |
|---|
Attached slightly improved patch. ModelChoiceField and ModelMultipleChoiceFields need tests.
by , 18 years ago
| Attachment: | ModelChoiceField_update_choices.diff added |
|---|
comment:8 by , 18 years ago
Marked #5913 as a duplicate. It exposes some functionality that this patch should include.
comment:9 by , 18 years ago
Since my ticket was marked as duplicate which i don't completely agree with because my problem is with incomplete cleaning and this one is with attribute changing I'm attaching a patch consisting of eads's patch and mine
by , 18 years ago
| Attachment: | ModelChoiceField_update_choices_and_fix_clean_method.patch added |
|---|
ead's patch and some modification in the clean() method
comment:11 by , 18 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
(In [6670]) Fixed #4787, #5913 -- Updating the queryset on a ModelChoiceField or ModelMultipleChoiceField now updates its widget's choices. The clean methods for ModelChoiceField and ModelMultipleChoiceField were changed to only allow choices in the specified queryset (instead of allowing all choices returned by the queryset model's default manager).
Please attach a diff. Some tests would be nice too.