Opened 17 years ago

Closed 17 years ago

#6839 closed (invalid)

using label_from_instance on ModelChoiceField requires resetting queryset first

Reported by: Koen Biermans <koen.biermans@…> Owned by: nobody
Component: Forms Version: dev
Severity: Keywords: ModelChoiceField
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Overriding the new label_from_instance on ModelChoiceField (introduced in r7326) requires also resetting the queryset on the field for the new label_from_instance function to be used.
A short example:

class Aform(forms.Form):
    user = forms.ModelChoiceField(queryset=User.objects.all())

    def __init__(self, *args, **kwargs):
        super(Aform, self).__init__(*args, **kwargs)
        # without the next line label_from_instance does NOT work
        self.fields['user'].queryset = User.objects.all()
        self.fields['user'].label_from_instance = lambda obj: "%s %s" % (obj.last_name, obj.first_name)

If I do not redefine the queryset in init, it does not work. If I do, it does.

Change History (1)

comment:1 by James Bennett, 17 years ago

Resolution: invalid
Status: newclosed

The idea here is to subclass ModelChoiceField and override label_from_instance. Which, coincidentally, is precisely what the docstring on label_from_instance says you should do.

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