So your model is like:

    class Folder(models.Model):
        user = models.ForeignKey(User)
        # yadda yadda

And you want to give the user a form to sort an object into one of
their own folders. So what you do is make a form like this:

    class ObjectSortForm(forms.Form):
        folder = models.ModelChoiceField(Folder.objects.all())

But, if your view, you don't actually want Folder.objects.all() - you
want user.folder_set.all(). So, you have to set the
form.fields['folder'].queryset attribute like this in your view:

    def my_view(request):
        if request.method=="POST":
            # yadda yadda form handling...
        else:
            form = ObjectSortForm()
            form.fields['folder'].queryset = request.user.folder_set.all()

        # etc etc...
        return render_to_response("my_template.html", dict(form=form))

Unfortunately, the docs are somewhat incomplete - they only explain
how to set the initial queryset. I've been meaning to write up some
documentation explaining the .queryset attribute, so thanks for
spurring some action :)

Adam


On Tue, May 20, 2008 at 8:59 AM, chylld <[EMAIL PROTECTED]> wrote:
>
> I have exactly the same problem; I just want to fill the ChoiceField
> with a certain subset of choices based on the current user. Have you
> found a way to do this??
>
> Jonathan
>
>
> On Apr 10, 7:21 am, ydjango <[EMAIL PROTECTED]> wrote:
>> Yes, They are defined in model as
>>
>> owner = models.ForeignKey(Participant,null=True)
>>
>> all I want is to display only the Partcipants for a particular group
>> in the choice field ( not all participants)
>>  and I am so frustrated now.
>>
>> eg. Participant.objects.filter(group__exact=my_group)]
>>
>> thanks
>> Ashish
>>
>> On Apr 9, 2:15 pm, "James Bennett" <[EMAIL PROTECTED]> wrote:
>>
>> > On Wed, Apr 9, 2008 at 3:30 PM, ydjango <[EMAIL PROTECTED]> wrote:
>> > >  Miles and Owner are defined as foreign_key to respective tables.
>>
>> > Yes, but did you actually look at the docs for the field types that
>> > make it easy to represent foreign keys?
>>
>> > --
>> > "Bureaucrat Conrad, you are technically correct -- the best kind of 
>> > correct.
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to