0

For a Course Site (created using flask), I have created a json database which I am accessing using pickleDB in main.py.

Here is how my course.json looks like:

{
  "Course A": 
  {"Student1": 
  {
    "grade":"",
    "distribution":"",
    "timezone":"",
    "rank":"",
  },
  "Student2":
  {
    "grade":"",
    "distribution":"",
    "timezone":"",
    "rank":"",
  }
  },
  
  "Course B": 
  {"Student1": 
  {
    "grade":"",
    "distribution":"",
    "timezone":"",
    "rank":"",
  },
  "Student2":
  {
    "grade":"",
    "distribution":"",
    "timezone":"",
    "rank":"",
  }
  }
}

I've added all necessary imports and set it up fine. Now, from a form, I requested the data (for a new student), and I want to add it to course A, and I've used the append feature of pickleDB like this:

  course.append(courseName, 
  {username: 
  {
    "grade":userGrade,
    "distribution":userDistribution,
    "timezone":userTimezone,
    "rank":userRank,
  }
  }
    )

however, when I do so, an error throws up. Here's the error:

[2021-06-17 08:59:08,203] ERROR in app: Exception on /admin/assign [POST]
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "main.py", line 398, in courseAdd
    course.append(courseName,
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pickledb.py", line 151, in append
    self.db[key] = tmp + more
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'

I don't know why i am getting the error, and I referred to pickleDB docs too: https://patx.github.io/pickledb/commands.html

/bump

1 Answers1

0

Try using

a = dict(list(data1.items()) + list(data2.items()))
print(a)

With data as pickle db data.

Madan Raj
  • 279
  • 4
  • 15