0

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?

Community
  • 1
  • 1
user2495766
  • 123
  • 3
  • 15
  • http://stackoverflow.com/questions/1010573/how-do-i-delete-all-entities-from-my-local-google-app-engine-datastore solve my problem... – user2495766 Jun 18 '13 at 08:18

3 Answers3

2
k = users.query(users.name == 'abhinav')
for l in k.fetch(limit = 1):
    l.key.delete()
HimalayanCoder
  • 9,630
  • 6
  • 59
  • 60
0

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.

oberbaum
  • 2,451
  • 7
  • 36
  • 52
Greivin López
  • 121
  • 1
  • 5
  • I think this is incorrect. delete() only exists on the key, so: `report.key.delete()` https://developers.google.com/appengine/docs/python/ndb/entities#deleting_entities – Jonny Jul 25 '14 at 08:25
  • @Jonny is right: AttributeError: 'tmpReport' object has no attribute 'delete' – Tjorriemorrie Jan 10 '15 at 17:48
0
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.

Zain Qasmi
  • 345
  • 2
  • 14