In Flask convert form POST object into a representation suitable for mongodb

后端 未结 6 1624
难免孤独
难免孤独 2020-12-03 02:09

I am using Flask and MongoDB. I am trying to convert the content of request.form into something suitable for saving via PyMongo. It seems like something that should come up

6条回答
  •  隐瞒了意图╮
    2020-12-03 02:59

    The Flask ImmutableMultiDict data structure has a built in to_dict method.

    This knowledge in addition to the Flask request object form property being an ImmutableMultiDict allows for simple handling of a form POST request to MongoDB.

    See below for a naive example:

    from flask import request
    
    @app.route('/api/v1/account', methods=['POST'])
    def create_account():
        """Create user account"""
        account_dict = request.form.to_dict()
    
        db.account.insert_one(account_dict)
    

提交回复
热议问题