doc/book/en/development/entityclasses/load-sort.rst
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 03 Aug 2009 09:24:08 +0200
changeset 2648 4ae7d02ce063
parent 2546 df456fa1b053
child 3177 e7ae807554d9
permissions -rw-r--r--
F [rset repo cache] set entity.rset when no set on entities retreived from the cache


.. _FetchAttrs:

Loaded attributes and default sorting management
````````````````````````````````````````````````

* The class attribute `fetch_attrs` allows to define in an entity class a list
  of names of attributes or relations that should be automatically loaded when
  entities of this type are fetched from the database. In the case of relations,
  we are limited to *subject of cardinality `?` or `1`* relations.

* The class method `fetch_order(attr, var)` expects an attribute (or relation)
  name as a parameter and a variable name, and it should return a string
  to use in the requirement `ORDERBY` of an RQL query to automatically
  sort the list of entities of such type according to this attribute, or
  `None` if we do not want to sort on the attribute given in the parameter.
  By default, the entities are sorted according to their creation date.

* The class method `fetch_unrelated_order(attr, var)` is similar to the
  method `fetch_order` except that it is essentially used to control
  the sorting of drop-down lists enabling relations creation in
  the editing view of an entity.

The function `fetch_config(fetchattrs, mainattr=None)` simplifies the
definition of the attributes to load and the sorting by returning a
list of attributes to pre-load (considering automatically the attributes
of `AnyEntity`) and a sorting function based on the main attribute
(the second parameter if specified otherwisethe first attribute from
the list `fetchattrs`).
This function is defined in `cubicweb.entities`.

For example: ::

  class Transition(AnyEntity):
    """..."""
    id = 'Transition'
    fetch_attrs, fetch_order = fetch_config(['name'])

Indicates that for the entity type "Transition", you have to pre-load
the attribute `name` and sort by default on this attribute.