Reputation: 123
I'm new to app-engine [Python 2.7] I would like to delete elements from my ndb (currently I don't care if it is one by one or all at once since none is working for me).
Version 1 based on this Q:
ps_ancestors = req_query.fetch()
for ps_ancestor in ps_ancestors:
self.response.write(ps_ancestor.key)
ps_ancestor.key.delete()
It continues to print the same data without actually deleting anything
Version 2: [myId currently have only the values 1,2,3]
ndb.Key(myId, 1).delete()
ndb.Key(myId, 2).delete()
ndb.Key(myId, 3).delete()
The model:
class tmpReport (ndb.Model):
myId = ndb.IntegerProperty()
hisId = ndb.IntegerProperty()
date = ndb.DateTimeProperty(auto_now_add=True)
What am I missing?
Upvotes: 0
Views: 2502
Reputation: 355
query1 = tmpReport.query()
query2 = query1.filter(tmpReport.myId == int(myId))
#Add other filters as necessary
query3 = query2.fetch()
query3[0].key.delete()
Removes the first entity(element) returned assuming myID is unique so there is only one element in the list.
Upvotes: 0
Reputation: 121
First of all, you should not define your Entity key as an IntegerProperty. Take a look at this documentation: NDB Entities and Keys
In order to delete an entity from datastore you should first retrieve it by using a query or by its ID. I recommend you to use a "keyname" when creating your entities (to use it as your custom ID):
# Model declaration
class tmpReport (ndb.Model):
hisId = ndb.IntegerProperty()
date = ndb.DateTimeProperty(auto_now_add=True)
# Store the entity
report = tmpReport(id=1, hisId=5)
report.put()
Then to retrieve and delete the previous entity use:
# Retrieve entity
report = ndb.Key("tmpReport", 1).get()
# Delete the entity
report.key.delete()
Hope it helps.
Upvotes: 0
Reputation: 9850
k = users.query(users.name == 'abhinav')
for l in k.fetch(limit = 1):
l.key.delete()
Upvotes: 2