User talk:Python

From Facebook Developer Wiki

Jump to: navigation, search

[edit] Is there a way to access the facebook API from within a model?

I find myself often having to write "model-like" code in my views just because there is no access to the Facebook API from the model. I understand that the authentication etc happens in the views but are there other reasons not to access the API from a model?

  • Can you give an example of this? I currently have my own "wrapper" models such as User and Affiliation, and a couple of convenience methods which get passed a Facebook object. Although I can see why this would be useful, you would need to have the session info in the model to make any API calls... --13608493 11:55, 30 June 2007 (PDT)
  • For me, I store user id and event id in my data model in django, then I have to make facebook api calls (from the view) to extract further information using these ids. One way to solve this would be to duplicate the data from facebook into my database, but this would lead to out of date and inconsistent data. Could you share your wrapper classes? --204500009 14:58, 30 June 2007 (PDT)
  • OK, I see what you mean now. I don't have anything similar to this right now, since most of what I need I can get with FBML. It might be nice to have the request.facebook object in thread-local storage, as explained here: [[1]]. This way it could be accessed from models as well. Past that, I'm not sure how useful including custom models in the library would be, because there would be a lot of stuff that wouldn't ever get used - writing the wrappers inside your models with this shouldn't be too hard. (Or maybe a model that has FQL as a backend.... :-)) --13608493 15:50, 30 June 2007 (PDT)
  • Thanks for your reply, I'm still unsure what you mean about writing wrappers inside the models, could you give me a quick example? Thanks for you work on this, much appreciated! --204500009 16:17, 30 June 2007 (PDT)
  • Thread locals seems like a good solution, I'm not sure of any others. It might just be best for me to bite my tongue and write some more into my view. I'm currently in a situation where I wish to display "name" in different ways depending on the value of "type" in my model. If "type" is EVENT then I need to do a facbook.events.get lookup, otherwise I just pull the value of "name" from the model. This means I have to do some messy stuff in my view: unpacking an objects.all() request and repacking it as another list, to pass to my templates.--204500009 08:26, 1 July 2007 (PDT)
  • The way I get round this problem is to have a function in my model, called prepare_for_view(self, request) which takes in the request variable and updates variables in the class. This seems to be the best way of doing it so far. --204500009 18:49, 1 July 2007 (PDT)
  • OK, I've added thread locals to SVN if you want to use that instead. Just call get_facebook_client() and you should get the current Facebook object for a request. --13608493 20:11, 1 July 2007 (PDT)
  • Thanks a lot, it works perfectly and seems to be the cleanest solution to this problem, now I don't have to pass "request" into my helper functions :). Thanks again for the great work on this module.--204500009 06:01, 4 July 2007 (PDT)

[edit] Example of application with app engine and django

http://code.google.com/p/theslap-engine/

maybe add a new section Facebook and Appengine ?

reference