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:
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:
This class method may be used to control sort order when multiple entities of this type are fetched through ORM methods. Its arguments are:
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()
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.