Opened 17 years ago

Closed 17 years ago

#4787 closed (fixed)

Updating queryset on a ModelChoiceField, ModelMultipleChoiceField

Reported by: fero <luca.ferroni@…> 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)

ModelChoiceField_widget_choices.patch (682 bytes ) - added by fero <luca.ferroni@…> 17 years ago.
Proposed patch
ModelChoiceField_update_choices.diff (1.2 KB ) - added by eads 17 years ago.
ModelChoiceField_update_choices_and_fix_clean_method.patch (2.2 KB ) - added by cohcheto@… 17 years ago.
ead's patch and some modification in the clean() method

Download all attachments as: .zip

Change History (14)

comment:1 by Gary Wilson <gary.wilson@…>, 17 years ago

Needs tests: set
Patch needs improvement: set
Triage Stage: UnreviewedAccepted

Please attach a diff. Some tests would be nice too.

by fero <luca.ferroni@…>, 17 years ago

Proposed patch

comment:2 by fero <luca.ferroni@…>, 17 years ago

Attached patch file, test will be supplied asap

comment:3 by peter.melvyn@…, 17 years ago

Keywords: ModelMultipleChoiceField added
Summary: Updating queryset on a ModelChoiceFieldUpdating queryset on a ModelChoiceField, ModelMultipleChoiceField

ModelMultipleChoiceField has the same problem

comment:4 by anonymous, 17 years ago

Cc: michal@… added

comment:5 by eads, 17 years ago

Owner: changed from nobody to eads

comment:6 by eads, 17 years ago

Patch needs improvement: unset

Attached slightly improved patch. ModelChoiceField and ModelMultipleChoiceFields need tests.

comment:7 by Gary Wilson, 17 years ago

#5830 marked as a dup.

comment:8 by Brian Rosner, 17 years ago

Marked #5913 as a duplicate. It exposes some functionality that this patch should include.

comment:9 by cohcheto@…, 17 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 cohcheto@…, 17 years ago

ead's patch and some modification in the clean() method

comment:10 by Gary Wilson, 17 years ago

Yes, let's keep the issues separate please. I've re-opened #5913.

comment:11 by Gary Wilson, 17 years ago

Resolution: fixed
Status: newclosed

(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).

Note: See TracTickets for help on using tickets.
Back to Top