Opened 8 years ago

Closed 7 years ago

#4787 closed (fixed)

Updating queryset on a ModelChoiceField, ModelMultipleChoiceField

Reported by: fero <luca.ferroni@…> Owned by: eads
Component: Forms Version: master
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: UI/UX:

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@…> 8 years ago.
Proposed patch
ModelChoiceField_update_choices.diff (1.2 KB) - added by eads 7 years ago.
ModelChoiceField_update_choices_and_fix_clean_method.patch (2.2 KB) - added by cohcheto@… 7 years ago.
ead's patch and some modification in the clean() method

Download all attachments as: .zip

Change History (14)

comment:1 Changed 8 years ago by Gary Wilson <gary.wilson@…>

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Accepted

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

Changed 8 years ago by fero <luca.ferroni@…>

Proposed patch

comment:2 Changed 8 years ago by fero <luca.ferroni@…>

Attached patch file, test will be supplied asap

comment:3 Changed 8 years ago by peter.melvyn@…

  • Keywords ModelMultipleChoiceField added
  • Summary changed from Updating queryset on a ModelChoiceField to Updating queryset on a ModelChoiceField, ModelMultipleChoiceField

ModelMultipleChoiceField has the same problem

comment:4 Changed 8 years ago by anonymous

  • Cc michal@… added

comment:5 Changed 7 years ago by eads

  • Owner changed from nobody to eads

comment:6 Changed 7 years ago by eads

  • Patch needs improvement unset

Attached slightly improved patch. ModelChoiceField and ModelMultipleChoiceFields need tests.

Changed 7 years ago by eads

comment:7 Changed 7 years ago by gwilson

#5830 marked as a dup.

comment:8 Changed 7 years ago by brosner

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

comment:9 Changed 7 years ago by cohcheto@…

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

Changed 7 years ago by cohcheto@…

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

comment:10 Changed 7 years ago by gwilson

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

comment:11 Changed 7 years ago by gwilson

  • Resolution set to fixed
  • Status changed from new to 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).

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