python convert object into json for mongodb

后端 未结 2 1141
臣服心动
臣服心动 2021-01-26 13:10

Folks, I have the following Class:

class User(object):
    def __init__(self, name, bd, phone, address):
        self.name = name
        self.bd = bd
                 


        
2条回答
  •  温柔的废话
    2021-01-26 13:35

    You have multiple options:

    1. Store the user data as a dictionary and then dump in into json. It is doable, but I don't recommend it.

    2. Use ORM (Object Relation Mapper) which bascially maps an object (such as user) into a table in the database. The defacto ORM for Python is SQLAlchemy. However, since you mention MongoDB I suggest take a look at mongokit.

提交回复
热议问题