2012-12-19 47 views
1

我創建了一個twitter.Twitter對象,但無法將其存儲到會話中。以下是我的代碼。django:爲什麼我無法將twitter.Twitter對象存儲到會話中

t = twitter.Twitter(auth=twitter.OAuth(consumer_key, consumer_secret, 
    oauth_token, oauth_secret))  
request.session['t'] = t 

,但如果我喜歡存儲

request.session['t'] = 't' 

它工作的字符串,爲什麼呢?因爲Twitter#Twitter對象不是可序列化的對象?每當我需要Twitter Twitter對象時,如何避免驗證?以下是django跟蹤。

Django Version: 1.3.1 
Python Version: 2.6.6 
Installed Applications: 
['django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.sites', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'myproj.myapp'] 
Installed Middleware: 
('django.middleware.common.CommonMiddleware', 
'django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware') 


Traceback: 
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response 
    178.     response = middleware_method(request, response) 
File "/usr/lib/python2.6/site-packages/django/contrib/sessions/middleware.py" in process_response 
    36.     request.session.save() 
File "/usr/lib/python2.6/site-packages/django/contrib/sessions/backends/file.py" in save 
    121.      os.write(output_file_fd, self.encode(session_data)) 
File "/usr/lib/python2.6/site-packages/django/contrib/sessions/backends/base.py" in encode 
    93.   pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL) 
File "/usr/lib/python2.6/site-packages/twitter-1.9.0-py2.6.egg/twitter/api.py" in __call__ 
    173.   return self._handle_response(req, uri, arg_data) 
File "/usr/lib/python2.6/site-packages/twitter-1.9.0-py2.6.egg/twitter/api.py" in _handle_response 
    198.     raise TwitterHTTPError(e, uri, self.format, arg_data) 

Exception Type: TwitterHTTPError at /tool/log_in/ 
Exception Value: 

回答

0

正如您猜到的那樣,您只能存儲pickleable對象。

the docs

會話字典可以接受任何與pickle Python對象。有關更多信息,請參閱pickle模塊。

+0

謝謝哈維爾,但什麼是解決方法,我怎樣才能避免驗證每次當我需要一個Twitter#Twitter對象?對不起,這似乎是另一個問題。 –

+0

存儲足夠的數據來重建對象。我不知道Twitter對象的細節,但應該有一些可序列化的Oauth鍵 – Javier

相關問題