Getting objects

To get an object by its primary key you need to specify the primary key value in the square brackets:

  1. >>> p1 = Person[1]
  2. >>> print p1.name
  3. 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:

  1. >>> mary = Person.get(name='Mary')
  2. SELECT "id", "name", "age"
  3. FROM "Person"
  4. WHERE "name" = ?
  5. [u'Mary']
  6. >>> print mary.age
  7. 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:

  1. >>> show(mary)
  2. instance of Person
  3. id|name|age
  4. --+----+---
  5. 2 |Mary|22