Search code examples
python-2.7google-app-enginegoogle-cloud-datastorewebapp2

Deleting Entities in NDB last child is always being deleted


I have models similar to the example found on the documentation for Python GAE (Greeting and Author). I have a problem when it comes to deletion because it is always the last child that is being deleted and not the entity that I was targeting.

class Delete(webapp2.RequestHandler):
     def post(self):
         greeting_key = ndb.Key(urlsafe=self.request.get('for_deletion')) #it's the key's urlsafe that is being passed here
         greeting_key.delete()
         self.redirect('/events')

Here is a code snippet of what generates the request:

               self.response.write('''<br><br><form action="/delete" method="post">        
                   <input type="hidden" name="for_deletion" value="%s">
                   <input type="submit" value="Delete Most Recent Info"><br><br>
               <form>
               ''' % post.key.urlsafe())

Solution

  • The problem arised from the fact that I have a lot of hidden inputs with the name="for_deletion". The server script chooses that last in queue that's why I get to have the last child deleted.

    I have modified that html page that generates the request in a sense that I used javascript to ask the user if he/she wants to delete the data and then I would go on about sending a form with a hidden input named "for_deletion". In that way I will ensure that there is only one html element name="for_deletion".

    Here is a code snippet of the js file:

             document.body.innerHTML += '<form id="dynForm" action="/delete" method="post"><input type="hidden" name="for_deletion" value=' + urlsafe + '></form>';
             document.getElementById("dynForm").submit();