|
1 |
|
2 .. _FetchAttrs: |
|
3 |
|
4 Loaded attributes and default sorting management |
|
5 ```````````````````````````````````````````````` |
|
6 |
|
7 * The class attribute `fetch_attrs` allows to define in an entity class a list of |
|
8 names of attributes that should be automatically loaded when entities of this |
|
9 type are fetched from the database using ORM methods retrieving entity of this |
|
10 type (such as :meth:`related` and :meth:`unrelated`). You can also put relation |
|
11 names in there, but we are limited to *subject relations of cardinality `?` or |
|
12 `1`*. |
|
13 |
|
14 * The :meth:`cw_fetch_order` and :meth:`cw_fetch_unrelated_order` class methods |
|
15 are respectively responsible to control how entities will be sorted when: |
|
16 |
|
17 - retrieving all entities of a given type, or entities related to another |
|
18 |
|
19 - retrieving a list of entities for use in drop-down lists enabling relations |
|
20 creation in the editing view of an entity |
|
21 |
|
22 By default entities will be listed on their modification date descending, |
|
23 i.e. you'll get entities recently modified first. While this is usually a good |
|
24 default in drop-down list, you'll probably want to change `cw_fetch_order`. |
|
25 |
|
26 This may easily be done using the :func:`~cubicweb.entities.fetch_config` |
|
27 function, which simplifies the definition of attributes to load and sorting by |
|
28 returning a list of attributes to pre-load (considering automatically the |
|
29 attributes of `AnyEntity`) and a sorting function as described below: |
|
30 |
|
31 .. autofunction:: cubicweb.entities.fetch_config |
|
32 |
|
33 In you want something else (such as sorting on the result of a registered |
|
34 procedure), here is the prototype of those methods: |
|
35 |
|
36 |
|
37 .. automethod:: cubicweb.entity.Entity.cw_fetch_order |
|
38 |
|
39 .. automethod:: cubicweb.entity.Entity.cw_fetch_unrelated_order |
|
40 |