Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'ImmutableMultiDict' objects are immutable python

Tags:

python

flask

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

like image 829
MadzQuestioning Avatar asked Aug 16 '17 11:08

MadzQuestioning


1 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']
like image 98
bgse Avatar answered Oct 09 '22 20:10

bgse