Opened 3 years ago

Closed 3 years ago

#32912 closed Bug (duplicate)

Using label_from_instance on ModelChoiceField requires resetting queryset first

Reported by: mbehm Owned by: nobody
Component: Forms Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The part in the following old issue about having to reset queryset for the label_from_instance to work is valid, even if you follow the documentation to the letter your custom version of label_from_instance won't be called unless you also define the queryset, even if the queryset would be the default.

https://code.djangoproject.com/ticket/6839

Change History (1)

comment:1 by Mariusz Felisiak, 3 years ago

Resolution: duplicate
Status: newclosed

I'm sorry but I don't see anything new in your description. As far as I'm aware you don't need do "reset" a queryset when subclassing ModelChoiceField as documented, e.g.

from django.forms import ModelChoiceField

class MyModelChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return "My Object #%i" % obj.id

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

Duplicate of #6839.

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