Google Groups Home
Help | Sign in
label_from_instance and queryset for ModelChoiceField (r7326)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
koenb  
View profile
 More options Mar 20, 8:50 am
From: koenb <koen.bierm...@werk.belgie.be>
Date: Thu, 20 Mar 2008 05:50:47 -0700 (PDT)
Local: Thurs, Mar 20 2008 8:50 am
Subject: label_from_instance and queryset for ModelChoiceField (r7326)
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Bennett  
View profile
 More options Mar 20, 12:02 pm
From: "James Bennett" <ubernost...@gmail.com>
Date: Thu, 20 Mar 2008 11:02:15 -0500
Local: Thurs, Mar 20 2008 12:02 pm
Subject: Re: label_from_instance and queryset for ModelChoiceField (r7326)

On Thu, Mar 20, 2008 at 7:50 AM, koenb <koen.bierm...@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."


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
koenb  
View profile
 More options Mar 21, 5:07 am
From: koenb <koen.bierm...@werk.belgie.be>
Date: Fri, 21 Mar 2008 02:07:34 -0700 (PDT)
Local: Fri, Mar 21 2008 5:07 am
Subject: Re: label_from_instance and queryset for ModelChoiceField (r7326)
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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google