label_from_instance and queryset for ModelChoiceField (r7326)

338 views
Skip to first unread message

koenb

unread,
Mar 20, 2008, 8:50:47 AM3/20/08
to Django users
Hi all,

has anyone used label_from_instance yet for ModelChoiceField
(introduced in r7326)?

It seems to get it to work, I have to redefine the queryset on the
field first.

What I mean is:

suppose I have a form (newforms that is)

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, it does not work. If I do, it does.
Is this a bug or was this not the intended use for
label_from_instance ?

Koen

James Bennett

unread,
Mar 20, 2008, 12:02:15 PM3/20/08
to django...@googlegroups.com
On Thu, Mar 20, 2008 at 7:50 AM, koenb <koen.b...@werk.belgie.be> wrote:
> If I do not redefine the queryset, it does not work. If I do, it does.
> Is this a bug or was this not the intended use for
> label_from_instance ?

In two separate places in the changeset, it's explained that you
should subclass ModelChoiceField and override the label_from_instance
method.


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

koenb

unread,
Mar 21, 2008, 5:07:34 AM3/21/08
to Django users
Aha, that makes sense. I tried that and it works like a charm.

Thanks,

Koen

On 20 mrt, 17:02, "James Bennett" <ubernost...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages