简体   繁体   中英

How to save a dictionary having value as an object

binHow to save this kind of dictionary in mongiengine.

{'xnFH8XCJiwMRFu8A': <dropbox.session.OAuthToken object at 0x2e4efd0>,  
 '867s6bnwbg3kc':<dropbox.session.OAuthToken object at 0x2e50610>}

I am trying to save the dropbox access token.

Thanks for the help in advance.

Thanks for the help.

But getting the error InvalidStringData. No idea where I am doing wrong.

code snippet is as follows:

  #TOKEN_STORE is the above dictionary
  dbsession = DBsession.objects.get(oauth_token=oauth_token) # oauth_token is is request

  v = pickle.dumps(TOKEN_STORE, -1)
  print v # this prints proper binary string
  print pickle.loads(v) # this prints proper values

  dbsession.update(set__token_store=v) # this line fails and throws error on  
                                       #InvalidStringData


#DBSession Definition
# me is mongoengine
class DBSession(me.Document):
    oauth_token = me.StringField(required=True)
    token_store = me.BinaryField()

This shows exactly how to connect to mongodb and how to store stuff there.
Plz read: http://docs.mongoengine.org/en/latest/apireference.html#documents

I would however not recommend using mongodb if you're using it solely for this. If you really only want to save his dict in a persistent way and you only need to access it from python you might as well use shelve http://docs.python.org/2/library/shelve.html

Update after question has been updated:

I think it should be

dbsession.update(set__token_store=pickle.loads(v))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM