I created one table in Google App Engine. I stored and retrieved data from Google App Engine.
However, I don't know how to delete data from Google App Engine Datastore.
An application can delete an entity from the datastore using a model instance or a Key. The model instance's delete() method deletes the corresponding entity from the datastore. The delete() function takes a Key or list of Keys and deletes the entity (or entities) from the datastore:
q = db.GqlQuery("SELECT * FROM Message WHERE msg_date < :1", earliest_date)
results = q.fetch(10)
for result in results:
result.delete()
# or...
q = db.GqlQuery("SELECT __key__ FROM Message WHERE msg_date < :1", earliest_date)
results = q.fetch(10)
db.delete(results)
Source and further reading:
Google App Engine: Creating, Getting and Deleting Data
If you want to delete all the data in your datastore, you may want to check the following Stack Overflow post:
How to delete all datastore in Google App Engine?
You need to find the entity then you need delete it.
So in python it would be
q = db.GqlQuery("SELECT __key__ FROM Message WHERE create_date < :1", earliest_date)
results = q.get()
db.delete(results)
or in Java it would be
pm.deletePersistent(results);
URLS from app engine are
http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html#Deleting_an_Object
http://code.google.com/appengine/docs/python/datastore/creatinggettinganddeletingdata.html#Deleting_an_Entity
In Java
I am assuming that you have an endpoint:
Somethingendpoint endpoint = CloudEndpointUtils.updateBuilder(endpointBuilder).build();
And then:
endpoint.remove<ModelName>(long ID);
Related
I've created two different Entities, one a User and one a Message they can create. I assign each user an ID and then want to assign this ID to each message which that user creates. How can I go about this? Do I have to do it in a query?
Thanks
Assuming that you are using Python NDB, you can having something like the following:
class User(ndb.Model):
# put your fileds here
class Message(ndb.Model):
owner = ndb.KeyProperty()
# other fields
Create and save a User:
user = User(field1=value1, ....)
user.put()
Create and save a Message:
message = Message(owner=user.key, ...)
message.put()
Query a message based on user:
messages = Message.query().filter(Message.owner==user.key).fetch() # returns a list of messages that have this owner
For more information about NDB, take a look at Python NDB API.
Also, you should take a look at Python Datastore in order to get a better understanding of data modeling in App Engine.
After creating a document with a explicit id like this:
Document(doc_id = 123)
Is it possible to find that document using the id, similar to get_by_id with the Datastore?
I've tried queries with id:123, _id:123, doc_id:123, and _doc_id:123.
Index.list_documents has the options start_doc_id and limit which can be used to get a single document by id.
documents = index.list_documents(start_doc_id = id, limit = 1)
I also found in Java that you can do index.get(ID), which might be useful for Java users.
Here's the Javadoc:
https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/search/Index#get(java.lang.String)
I would like to rewrite the example from the GAE djangoforms article to be show most up to date after submitting a form (e.g. when updating or adding a new entry) on Google App Engine using the High Replication Datastore.
The main recurring query in this article is:
query = db.GqlQuery("SELECT * FROM Item ORDER BY name")
which we will translate to:
query = Item.all().order('name') // datastore request
This query I would like to get the latest updated data from the high replication datastore after submitting the form (only in these occasions, I assume I can redirect to a specific urls after submission which just uses the query for the latest data and in all other cases I would not do this).
validating the form storing the results happens like:
data = ItemForm(data=self.request.POST)
if data.is_valid():
# Save the data, and redirect to the view page
entity = data.save(commit=False)
entity.added_by = users.get_current_user()
entity.put() // datastore request
and getting the latest entry from the datastore for populating a form (for editing) happens like:
id = int(self.request.get('id'))
item = Item.get(db.Key.from_path('Item', id)) // datastore request
data = ItemForm(data=self.request.POST, instance=item)
So how do I add entity groups/ancestor keys to these datastore queries to reflect the latest data after form submission. Please note, I don't want all queries to have the latest data, when populating a form (for editing) and after submitting a form.
Who can help me with practical code examples?
If it is in the same block, you have reference of the current intance.
Then once you put() it, you can get its id by:
if data.is_valid():
entity = data.save(commit=False)
entity.added_by = users.get_current_user()
entity.put()
id= entity.key().id() #this gives you inserted data id
I'm trying to create a site which is quite similar to Twitter. Users will be able to post messages. And users will be able to 'follow' each other. On the homepage, they see the messages from the users they follow, sorted by time.
How do I go about creating the appengine models for this?
In a traditional relational DB, i guess it would be something like this:
Database 'user':
id
username
Database 'follows':
user_id
follow_id
Database 'messages':
user_id
message
And the query will be something like:
SELECT * FROM messages m, follows f WHERE m.user_id = f.follow_id AND f.user_id = current_user_id
I guess i was clear with the example above. How do I replicate this in Google App Engine?
There is a useful presentation at Google I/O a while back by Brett Slatkin which describes building a scalable twitter-like microblog app, and deals with this very question at length: http://www.google.com/events/io/2009/sessions/BuildingScalableComplexApps.html
REVISED:
class AppUser(db.Model):
user_id = db.UserProperty()
username = db.StringProperty()
following = db.ListProperty(db.Key) # list of AppUser keys
class Message(db.Model):
sender = db.ReferenceProperty(AppUser)
body = db.TextProperty()
You would then query the results in two steps:
message_list = []
for followed_user in current_user.following:
subresult = db.GqlQuery("SELECT __key__ FROM Message WHERE sender = :1", followed_user)
message_list.extend(subresult)
results = Message.get(message_list)
(with 'current_user' being the 'AppUser' entity corresponding with your active user)
In Google App Engine, consider the following datastore model:
class Update(db.Model):
content = db.TextProperty()
date = db.DateTimeProperty()
source = db.StringProperty()
To add a new record, I do something like:
db.put(Update(content=..., date=..., source=...))
How can I add a record to the datastore only if it doesn't exist yet? What is the most efficient way to do this?
db.Model.get_or_insert(key_name) lets you pass the key name of the object to get or insert (think of it like a primary key)
More information about key_name