简体   繁体   English

如何保存具有值作为对象的字典

[英]How to save a dictionary having value as an object

binHow to save this kind of dictionary in mongiengine. bin如何在mongiengine中保存这种字典。

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

I am trying to save the dropbox access token. 我正在尝试保存Dropbox访问令牌。

Thanks for the help in advance. 我在这里先向您的帮助表示感谢。

Thanks for the help. 谢谢您的帮助。

But getting the error InvalidStringData. 但是得到错误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. 这显示了如何连接到mongodb以及如何在那里存储东西。
Plz read: http://docs.mongoengine.org/en/latest/apireference.html#documents Plz阅读: http//docs.mongoengine.org/en/latest/apireference.html#documents

I would however not recommend using mongodb if you're using it solely for this. 但是,如果您仅使用mongodb,我建议不要使用mongodb。 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 如果你真的只想以持久的方式保存他的dict并且你只需要从python访问它你也可以使用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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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