Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to access ID property from a datastore entity

Using Google App Engine SDK and Python, I'm facing an issue : I'm unable to access the ID property of a given entity properties. The only properties I can access are those defined in my class Model, plus the key property (see answer below) :

class Question(db.Model):
    text = db.StringProperty()
    answers = db.StringListProperty()
    user = db.UserProperty()
    datetime = db.DateTimeProperty()

I can access text, answers, user, datetime and key properties just fine. However, I can't access the ID property. For example, after fetching all entities (using Question.all()) :

# OK Within a template, this will return a string :
{{ question.text }}
# OK, this will return the entity key :
{{ question.key }}

# KO this will return nothing :
{{ question.id }}

Any ideas ? Thanks !

like image 454
jbmusso Avatar asked Feb 07 '10 22:02

jbmusso


People also ask

What is Datastore entity?

Data objects in Datastore are known as entities. An entity has one or more named properties, each of which can have one or more values. Entities of the same kind do not need to have the same properties, and an entity's values for a given property do not all need to be of the same data type.

How do I update entity in Datastore?

Updating entities To update an existing entity, modify the attributes of the Entity object, then pass it to the DatastoreService. put() method. The object data overwrites the existing entity. The entire object is sent to Datastore with every call to put() .

What are the properties of entities in database?

Attributes are properties of entities.


1 Answers

According to the documentation, there is no id() instance method defined for Model subclasses.

Try {{ question.key }} instead.

Also note that the key is not created until the entity is saved to the datastore.


Edit: more info based on OP's edit:

Since we're really after the numeric ID, we could do something like this in our template:

{{ question.key.id }}

Another note: you should never expect numeric IDs to increase in value corresponding with the order of entity creation. In practice, this is usually—but not always—the case.

like image 52
mechanical_meat Avatar answered Sep 23 '22 07:09

mechanical_meat