tags:

views:

91

answers:

1

I'm still getting to grips with Django and, in particular, Forms.

I created MyForm which subclasses forms.Form in which I define a field like this :

owner = forms.CharField(widget=forms.HiddenInput)

When I create a new, blank instance of the Form I want to prefill this with the creator's profile, which I'm doing like this :

form = MyForm( {'owner' : request.user.get_profile()} )

Which I imagine sets the owner field of the form to the request.user's id. (The type of the corresponding "owner" field in the models.Model class is ForeignKey of Profile.)

Before rendering the form, I need to check one piece of information about the owner. So I try to access form.owner, but there seems to be no "owner" attribute of the form object. I also tried form.instance.owner, but similarly, no luck.

What am I doing wrong? What have I misunderstood?

+1  A: 

You can access this value via the form's data dictionary:

form.data.get('owner')

Harold
thanks ... that's what I was looking for.
interstar
No prob. Glad I could be of help.
Harold