I am trying to delete a key value from a list.So i created a method in my class like this:

def deleteEmployee(self, employee):
    employee_to_Delete = ndb.Key(Employee, employee)
    if employee_to_delete in self.employees:
        idx=self.employees.index(employee_to_delete)
        del self.employees[idx]
        self.put()

and then in the method where i am calling this method, i have something like

class DeleteEmployeeHandler(webapp2.RequestHandler):
def post(self):
    employee_name = self.request.get('employee_name')
    employee=Employee.get_by_id(employee_name)
    emp_dept=employee.department
    dept=Department.get_or_insert(emp_dept)
    dept.deleteEmployee(employee)
    employee.delete()

but I get the error message

TypeError: key id must be a string or a number. Can someone please tell me why this is happening?

有帮助吗?

解决方案

employee is an instance of the Employee class, and you're passing that as the second parameter to ndb.Key. But the error message tells you exactly what's wrong: that parameter should be a string or an int, not an instance.

But, since you actually have the instance already, you don't need to construct a new Key: the employee object already has one, which you can use to delete.

employee_to_Delete = employee.key

其他提示

The error you get is from google's datastore, not from Python's list.

You call ndb.Key(Employee, employee) and my guess is the employee you pass in is wrong type.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top