On Sat, 2007-11-03 at 03:55 -0700, [EMAIL PROTECTED] wrote:
> i want to filter some object related to current logined user in
> forms.py, the key code is:
>  choices = [(str(x.id), x.name) for x in
> Category.objects.filter(user__id__exact=request.user.id)]
> 
> but i got this error:
> Exception Type:       NameError
> Exception Value:      global name 'request' is not defined
> Exception Location:   d:\django\mysite\blog\forms.py in __init__, line
> 16
> 
> my question is how to select current user's object in the
> forms.py,thanks!

You are only going to have access to 'request' inside your view function
and it is a local variable at that point. So you could create a method
on your CreateEntryForm class that took 'request' as an argument and set
up the category choices. Then something like this would work in your
view:

        def my_view(request, ....):
            form = CreateEntryForm()
            form.setup_choices(request)
            ...
        
Alternatively, you could do some fancy juggling of kwargs in
CreateEntryForm.__init__ so that you can pass 'request' into the
__init__ method (pop the 'request' keyword argument from **kwargs before
passing it up to the super's __init__ method.
        
Regards,
Malcolm

> 
> full code as below:
> 
> class CreateEntryForm(forms.Form):
>         def __init__( self, *args, **kwargs ):
>            super( CreateEntryForm, self ).__init__( *args, **kwargs )
>            self.fields['category'] = forms.ChoiceField(
>                 choices = [(str(x.id), x.name) for x in
> Category.objects.filter(user__id__exact=request.user.id)]
>                 )
> 
> 
>       category   = forms.CharField(max_length=80, label=_('Category'))
>       title      = forms.CharField(max_length=80, label=_('Title'))
> 
> 
> > 
> 
-- 
Everything is _not_ based on faith... take my word for it. 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
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