Django

Code

Ticket #4787 (closed: fixed)

Opened 1 year ago

Last modified 8 months ago

Updating queryset on a ModelChoiceField, ModelMultipleChoiceField

Reported by: fero <luca.ferroni@labs.it> Assigned to: eads
Milestone: Component: django.newforms
Version: SVN Keywords: ModelChoiceField ModelMultipleChoiceField Queryset
Cc: michal@logix.cz Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 0

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

ModelChoiceField_widget_choices.patch (0.7 kB) - added by fero <luca.ferroni@labs.it> on 07/12/07 03:36:10.
Proposed patch
ModelChoiceField_update_choices.diff (1.2 kB) - added by eads on 09/14/07 17:17:45.
ModelChoiceField_update_choices_and_fix_clean_method.patch (2.2 kB) - added by cohcheto@gmail.com on 11/10/07 09:45:27.
ead's patch and some modification in the clean() method

Change History

07/09/07 23:04:11 changed by Gary Wilson <gary.wilson@gmail.com>

  • needs_better_patch set to 1.
  • stage changed from Unreviewed to Accepted.
  • needs_tests set to 1.
  • needs_docs changed.

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

07/12/07 03:36:10 changed by fero <luca.ferroni@labs.it>

  • attachment ModelChoiceField_widget_choices.patch added.

Proposed patch

07/12/07 03:37:23 changed by fero <luca.ferroni@labs.it>

Attached patch file, test will be supplied asap

08/08/07 04:39:21 changed by peter.melvyn@gmail.com

  • keywords changed from ModelChoiceField Queryset to ModelChoiceField ModelMultipleChoiceField Queryset.
  • summary changed from Updating queryset on a ModelChoiceField to Updating queryset on a ModelChoiceField, ModelMultipleChoiceField.

ModelMultipleChoiceField? has the same problem

08/17/07 07:57:56 changed by anonymous

  • cc set to michal@logix.cz.

09/14/07 13:52:29 changed by eads

  • owner changed from nobody to eads.

09/14/07 17:14:26 changed by eads

  • needs_better_patch deleted.

Attached slightly improved patch. ModelChoiceField? and ModelMultipleChoiceFields? need tests.

09/14/07 17:17:45 changed by eads

  • attachment ModelChoiceField_update_choices.diff added.

10/29/07 19:06:33 changed by gwilson

#5830 marked as a dup.

11/09/07 14:08:50 changed by brosner

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

11/10/07 09:44:06 changed by cohcheto@gmail.com

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

11/10/07 09:45:27 changed by cohcheto@gmail.com

  • attachment ModelChoiceField_update_choices_and_fix_clean_method.patch added.

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

11/10/07 09:52:04 changed by gwilson

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

11/13/07 08:36:29 changed by gwilson

  • status changed from new to closed.
  • resolution set to fixed.

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


Add/Change #4787 (Updating queryset on a ModelChoiceField, ModelMultipleChoiceField)




Change Properties
Action