user3167683 user3167683 - 2 months ago 8
Python Question

Google App Engine Datastore delete entity by properties

I made a simple login page with

python
and
datastore
. the User model is the following.

class User(db.Model):
name = db.StringProperty()
password = db.StringProperty()


Question 1:

Lets say that I know the name and the password of a specific user. How can I delete that entity from the database that has the specified name and password properties?

Question 2:

Lets say that I have a User entity with "John" as the name and "1234" as the password. Given these 2 things, how can I change the name property "John" to "Marcus"?

Question 3:

Is it possible to achieve this without using
GqlQuery
?

Answer

These things are very well explained in the documentation. (Which also states clearly that you should not be using the old db API, but the ndb one, but never mind.)

1.

user = User.all().filter('name =', name).filter('password =', password).get()
if user:
    user.delete()

2.

user = User.all().filter('name =', 'John').filter('password =', '1234').get()
if user:
    user.name = 'Marcus'
    user.put()
  1. There is nothing that requires GqlQuery. GQL is simply another interface to the underlying datastore RPCs: it is not in any sense more "native" than the db or ndb APIs, and those do not "compile down" to GQL in the same way that (eg) Django's ORM compiles down to SQL.
Comments