4.5. Loaded attributes and default sorting management

  • The class attribute fetch_attrs allows to define in an entity class a list of names of attributes that should be automatically loaded when entities of this type are fetched from the database using ORM methods retrieving entity of this type (such as related() and unrelated()). You can also put relation names in there, but we are limited to subject relations of cardinality `?` or `1`.
  • The cw_fetch_order() and cw_fetch_unrelated_order() class methods are respectively responsible to control how entities will be sorted when:
    • retrieving all entities of a given type, or entities related to another
    • retrieving a list of entities for use in drop-down lists enabling relations creation in the editing view of an entity

By default entities will be listed on their modification date descending, i.e. you’ll get entities recently modified first. While this is usually a good default in drop-down list, you’ll probably want to change cw_fetch_order.

This may easily be done using the fetch_config() function, which simplifies the definition of attributes to load and sorting by returning a list of attributes to pre-load (considering automatically the attributes of AnyEntity) and a sorting function as described below:

cubicweb.entities.fetch_config(fetchattrs, mainattr=None, pclass=<class 'cubicweb.entities.AnyEntity'>, order='ASC')[source]

function to ease basic configuration of an entity class ORM. Basic usage is:

class MyEntity(AnyEntity):

    fetch_attrs, cw_fetch_order = fetch_config(['attr1', 'attr2'])
    # uncomment line below if you want the same sorting for 'unrelated' entities
    # cw_fetch_unrelated_order = cw_fetch_order

Using this, when using ORM methods retrieving this type of entity, ‘attr1’ and ‘attr2’ will be automatically prefetched and results will be sorted on ‘attr1’ ascending (ie the first attribute in the list).

This function will automatically add to fetched attributes those defined in parent class given using the pclass argument.

Also, You can use mainattr and order argument to have a different sorting.

In you want something else (such as sorting on the result of a registered procedure), here is the prototype of those methods:

classmethod Entity.cw_fetch_order(select, attr, var)[source]

This class method may be used to control sort order when multiple entities of this type are fetched through ORM methods. Its arguments are:

  • select, the RQL syntax tree
  • attr, the attribute being watched
  • var, the variable through which this attribute’s value may be accessed in the query

When you want to do some sorting on the given attribute, you should modify the syntax tree accordingly. For instance:

from rql import nodes

class Version(AnyEntity):
    __regid__ = 'Version'

    fetch_attrs = ('num', 'description', 'in_state')

    @classmethod
    def cw_fetch_order(cls, select, attr, var):
        if attr == 'num':
            func = nodes.Function('version_sort_value')
            func.append(nodes.variable_ref(var))
            sterm = nodes.SortTerm(func, asc=False)
            select.add_sort_term(sterm)

The default implementation call cw_fetch_unrelated_order()

classmethod Entity.cw_fetch_unrelated_order(select, attr, var)[source]

This class method may be used to control sort order when multiple entities of this type are fetched to use in edition (e.g. propose them to create a new relation on an edited entity).

See cw_fetch_unrelated_order() for a description of its arguments and usage.

By default entities will be listed on their modification date descending, i.e. you’ll get entities recently modified first.

Previous topic

4.1. Access to persistent data

Next topic

4.6. Interfaces and Adapters