문제

I have two classes, Department and Employee. Department has a property declared as

employees = ndb.KeyProperty(kind=ClassB, repeated=True)

The problem is,when i delete the entity whose key is held in the employees list, the entity is deleted in the Employee datastore, but the list in Department datastore remains the same (with the key of the deleted employee still in it).

How do i make sure that when the Employee is deleted, all references to it in the Department datastore is deleted as well?

도움이 되었습니까?

해결책 2

I finally found a way to do this.I created a method in class A (Department) like this

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

and then from the Handler(could also be another method somewhere) where i am deleting the key from the list,i did something like this

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)  #delete employee key from list
    employee.key.delete()  #then finally delete employee entity

다른 팁

There is no automatic way of doing this.

You need to perform queries for all types that could hold the key and then delete them in code.

If there could be a lot and/or it could take a long time you might want to consider using a task.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top