doc/book/en/development/entityclasses/load-sort.rst
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 03 Feb 2010 18:44:24 +0100
changeset 4437 21f2e01fdd6a
parent 3177 e7ae807554d9
child 4446 a413fac5ff5e
permissions -rw-r--r--
update exemples using the 3.6 api and add/fix some sections (schema, vreg, talk about CW_MODE in concepts...). So much to do :'(
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2546
df456fa1b053 [doc] more improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2539
diff changeset
     1
df456fa1b053 [doc] more improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2539
diff changeset
     2
.. _FetchAttrs:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     3
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     4
Loaded attributes and default sorting management
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     5
````````````````````````````````````````````````
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     6
2539
0f26a76b0348 [doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
     7
* The class attribute `fetch_attrs` allows to define in an entity class a list
0f26a76b0348 [doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
     8
  of names of attributes or relations that should be automatically loaded when
0f26a76b0348 [doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
     9
  entities of this type are fetched from the database. In the case of relations,
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    10
  we are limited to *subject of cardinality `?` or `1`* relations.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    11
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    12
* The class method `fetch_order(attr, var)` expects an attribute (or relation)
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    13
  name as a parameter and a variable name, and it should return a string
4437
21f2e01fdd6a update exemples using the 3.6 api and add/fix some sections (schema, vreg, talk about CW_MODE in concepts...). So much to do :'(
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3177
diff changeset
    14
  to use in the._cwuirement `ORDERBY` of an RQL query to automatically
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    15
  sort the list of entities of such type according to this attribute, or
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    16
  `None` if we do not want to sort on the attribute given in the parameter.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    17
  By default, the entities are sorted according to their creation date.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    18
3177
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    19
* The class method `fetch_unrelated_order(attr, var)` is similar to
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    20
  the method `fetch_order` except that it is essentially used to
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    21
  control the sorting of drop-down lists enabling relations creation
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    22
  in the editing view of an entity. The default implementation uses
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    23
  the modification date. Here's how to adapt it for one entity (sort
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    24
  on the name attribute): ::
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    25
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    26
   class MyEntity(AnyEntity):
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    27
       fetch_attrs = ('modification_date', 'name')
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    28
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    29
       @classmethod
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    30
       def fetch_unrelated_order(cls, attr, var):
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    31
           if attr == 'name':
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    32
              return '%s ASC' % var
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    33
           return None
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    34
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    35
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    36
The function `fetch_config(fetchattrs, mainattr=None)` simplifies the
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    37
definition of the attributes to load and the sorting by returning a
3177
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    38
list of attributes to pre-load (considering automatically the
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    39
attributes of `AnyEntity`) and a sorting function based on the main
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    40
attribute (the second parameter if specified, otherwise the first
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    41
attribute from the list `fetchattrs`). This function is defined in
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    42
`cubicweb.entities`.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    43
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    44
For example: ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    45
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    46
  class Transition(AnyEntity):
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    47
    """..."""
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    48
    id = 'Transition'
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    49
    fetch_attrs, fetch_order = fetch_config(['name'])
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    50
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    51
Indicates that for the entity type "Transition", you have to pre-load
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    52
the attribute `name` and sort by default on this attribute.