Of course form  does not have access to the object. But  you can  pass
some value which is accessible  in view from view to form.
 Separating  task in 2 parts :

 1)  set initial  values in  view class

class myviewclass():
""" This is example only . full class realization is more complex """

form_class = myformclass
template_name = "mytemplate.html"

get_initial(self):
""" Passing initial values to the form"""

# At first initial data should be saved
super(myviewclass, self).get_initial()
 # adding  our value
self.initial= {"myuser": self.request.user}
return self.initial


Second part (form side)

myformclass():
"""  This is really simple example. Perfect form is more complex"""
def __init__(self, *args, **kwargs):
# get our values from view
myvalues = kwargs.pop('initial', None)
#print it to console (this is a dict by default)
print myvalues
# and executing default constructor after this
super(myformclass, self).__init__(*args, **kwargs)



 Thanks, Serge


2012/3/11 Donald Stufft <donald.stu...@gmail.com>:
> Forms aren't only usable from inside a view.
>
> On Sunday, March 11, 2012 at 4:32 AM, shacker wrote:
>
> I recently needed to access request.user in a form, and found that I
> couldn't. Found many articles describing ways to accomplish this, such as
> James Bennett's [1].
>
> I did get it working, but I'm curious *why* the request object
> isn't accessible from forms as it is from views. Why do we need to override
> __init__  to accomplish this? Seems like the kind of thing Django could
> "take care of" for us.
>
> [1] http://www.b-list.org/weblog/2008/nov/09/dynamic-forms/
>
> Thanks to anyone who can clarify.
>
> ./s
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/1z9ElVqpRucJ.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>
> --
> 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
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to