Home > Blockchain >  AttributeError: 'MultiDict' object has no attribute 'iteritems' when moving from
AttributeError: 'MultiDict' object has no attribute 'iteritems' when moving from

Time:08-16

I am getting the following error. When running on localhost all works fine.

2022-08-13T14:17:31.683406 00:00 app[web.1]: [2022-08-13 14:17:31,682] ERROR in app: Exception on /create [GET]
2022-08-13T14:17:31.683432 00:00 app[web.1]: Traceback (most recent call last):
2022-08-13T14:17:31.683433 00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.10/site-packages/flask/app.py", line 2525, in wsgi_app
2022-08-13T14:17:31.683433 00:00 app[web.1]:     response = self.full_dispatch_request()
2022-08-13T14:17:31.683434 00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.10/site-packages/flask/app.py", line 1822, in full_dispatch_request
2022-08-13T14:17:31.683434 00:00 app[web.1]:     rv = self.handle_user_exception(e)
2022-08-13T14:17:31.683435 00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.10/site-packages/flask/app.py", line 1820, in full_dispatch_request
2022-08-13T14:17:31.683435 00:00 app[web.1]:     rv = self.dispatch_request()
2022-08-13T14:17:31.683436 00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.10/site-packages/flask/app.py", line 1796, in dispatch_request
2022-08-13T14:17:31.683436 00:00 app[web.1]:     return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
2022-08-13T14:17:31.683436 00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.10/site-packages/flask_login/utils.py", line 290, in decorated_view
2022-08-13T14:17:31.683437 00:00 app[web.1]:     return current_app.ensure_sync(func)(*args, **kwargs)
2022-08-13T14:17:31.683438 00:00 app[web.1]:   File "/app/app.py", line 235, in create
2022-08-13T14:17:31.683438 00:00 app[web.1]:     form = dynamic.process(CreateRecord, post)
2022-08-13T14:17:31.683439 00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.10/site-packages/wtforms_dynamic_fields/wtforms_dynamic_fields.py", line 110, in process
2022-08-13T14:17:31.683440 00:00 app[web.1]:     for field, data in post.iteritems():
2022-08-13T14:17:31.683440 00:00 app[web.1]: AttributeError: 'MultiDict' object has no attribute 'iteritems'

My create code is:

elif request.method == "GET":
    post = MultiDict()
    cfv = customfields.find({"parentRecord": current_user.user_json["_id"]})
    dynamic = WTFormsDynamicFields()

for value in cfv:
    if value['fieldType'] == "Text Field":
        post.add(value['fieldName'], "")
        dynamic.add_field(value['fieldName'], value['fieldName'], StringField, render_kw={"placeholder": "Enter "   value['fieldName']})
    elif value['fieldType'] == "Date Field":
        post.add(value['fieldName'], "")
        dynamic.add_field(value['fieldName'], value['fieldName'], DateField)
    elif value['fieldType'] == "Email Field":
        post.add(value['fieldName'], "")
        dynamic.add_field(value['fieldName'], value['fieldName'], EmailField, render_kw={"placeholder": "Enter "   value['fieldName']})
        dynamic.add_validator(value['fieldName'], Email)
form = dynamic.process(CreateRecord, post)
return render_template('create.html', form=form, auth=current_user.is_authenticated)

I am puzzled primarily because, as mentioned above, when running on the default flask dev webserver the app works perfectly fine without throwing code. I have deployed the app on Heroku and am now getting the error below.

CodePudding user response:

This was solved by creating a new class

from werkzeug.datastructures import ImmutableMultiDict

class Dict:
    '''Wrapper klass because iteritems no longer available in Python3.7 dict'''

    def __init__(self, mapping):
        self.mapping = ImmutableMultiDict(mapping)

    def iteritems(self):
        return self.mapping.items()

    def __getattr__(self, attr):
        return getattr(self.mapping, attr)

    def __iter__(self):
        return dict.__iter__(self.mapping)

And then adding post = Dict(post) prior to my process event.

  • Related