Answer a question

Hi I'm trying to add a new key value pair but I'm receiving an error of TypeError: 'ImmutableMultiDict' objects are immutable the variable I'm trying to add a new key came from a request.form but I can't add a new key value. Any idea on how to achieve this?

Here is my code on my controller

@benefits_api.route("/templates", methods=["POST"])
def store():

    parameters = request.form
    response = BenefitTemplateService.create(parameters)

    return jsonify(response), response['code']

and my service is like this

class BenefitTemplateService(object):

    @staticmethod
    def create(params):
        # some validation here

        params['credit_behavior'] = "none"
        return params

But I'm getting an error on the assignment for credit_behavior below is the error message

enter image description here

Answers

You can use the builtin to_dict() method of ImmutableMultiDict, this will provide you with a dict you can freely modify.

You could for example make the following change to your code:

@benefits_api.route("/templates", methods=["POST"])
def store():

    parameters = request.form.to_dict()
    response = BenefitTemplateService.create(parameters)

    return jsonify(response), response['code']
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐