Access to the request object from the form.

I use the pyfacebook module for Django to implement facebook Connect for one of my sites, I do not use the local User object, because I only want to use Facebook to login.

I understand that I cannot access the request object from the save method in the Django form, so how can I access the facebook object provided by the provided middleware?

I want to bind facebook UID users to an object created from the form, I could add it as a hidden field in the form, but then people could just change it so that it seems that the question came from someone else.

What is the best way to pass this uid to the save () method?

+4
source share
2 answers

The correct way to do this is to use an instance of the object you are trying to save, for example:

question = Question(user=int(request.facebook.uid)) form = QuestionForm(request.POST, instance=question) question = form.save() question.put() 

Do this in your view, not in the save () method of your object.

Keep track of whether any of the fields is required, you will need to specify them in the objector instance that calls form.save will throw an exception.

+2
source

You can set a variable in the form when you create it.

views.py

 def myview(request): form = FacebookConnectForm(request) 

forms.py

 class FacebookConnectForm(forms.Form): def __init__(self, instance): self.instance = instance def save(self): print self.instance ... 
+2
source

All Articles