Ojonugwa Jude Ochalifu
Ojonugwa Jude Ochalifu

Reputation: 27247

Python ndb ValueError: need more than 1 value to unpack

Am trying to retrieve an entity from the datastore, and then add a value to one of its properties but am getting an error saying

ValueError: need more than 1 value to unpack

Am performing this operation from a RequestHandler.This is my code

class AddNewEmployeeHandler(webapp2.RequestHandler):
def post(self):
    employee_name = self.request.get('employee_id')
    callback =self.request.get('callback')
    employee = Employee.get_by_id(employee_name)
    if employee:
        self.error(409)
    else:
        dept = Department.get_or_insert(key_name="Other Charges")
        dept.employees.append(employee)
        dept.put()

Other Charges is the id/name of an entity that already exists in the datastore, employees is a property of the Department class defined as

employees =ndb.keyProperty(repeated=True)

and i am getting the value of employee_id from a form.I have tried to find the solution to this online but i discovered that the error is thrown for a whole lot of reasons none of which is similar to my problem.Any suggestions as to why this is happening?

Edit enter image description here

Upvotes: 0

Views: 497

Answers (2)

Patrick Costello
Patrick Costello

Reputation: 3626

It looks like you are calling get_or_insert with key_name as a keyword argument. However, it expects it as a positional argument. See the docs here. Try this:

dept = Department.get_or_insert("Other Charges")

Upvotes: 1

radia
radia

Reputation: 1486

dept.employees is expecting keys, and you are passing an Employee entity, it should be :

dept.employees.append(employee.key)

To add Employee entity directly, it should be defined this way in your Department Model :

employees = StructuredProperty(Employee, repeated=True)

It depends on how you wanna stucture your Application Data Model

Upvotes: 1

Related Questions