Getting objects
To get an object by its primary key you need to specify the primary key value in the square brackets:
>>> p1 = Person[1]
>>> print p1.name
John
You may notice that no query was sent to the database. That happened because this object is already present in the database session cache. Caching reduces the number of requests that need to be sent to the database.
For retrieving the objects by other attributes, you can use the Entity.get()
method:
>>> mary = Person.get(name='Mary')
SELECT "id", "name", "age"
FROM "Person"
WHERE "name" = ?
[u'Mary']
>>> print mary.age
22
In this case, even though the object had already been loaded to the cache, the query still had to be sent to the database because the name
attribute is not a unique key. The database session cache will only be used if we lookup an object by its primary or unique key.
You can pass an entity instance to the show()
function in order to display the entity class and attribute values:
>>> show(mary)
instance of Person
id|name|age
--+----+---
2 |Mary|22