doc/book/en/devrepo/entityclasses/load-sort.rst
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Fri, 23 Apr 2010 17:31:46 +0200
branchstable
changeset 5394 105011657405
parent 4750 doc/book/en/development/entityclasses/load-sort.rst@875dc33551a9
child 7827 9bbf83f68bcc
permissions -rw-r--r--
[doc/book] move devweb up from development, turn development into devrepo (much better structure)
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
4446
a413fac5ff5e damn me, more stupid sed fix...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4437
diff changeset
    14
  to use in the requirement `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):
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
    27
       __regid__ = 'MyEntity'
3177
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    28
       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
    29
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    30
       @classmethod
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    31
       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
    32
           if attr == 'name':
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    33
              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
    34
           return None
e7ae807554d9 small note on fetch_unrelated_order ... fetch_order needs clarification
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2546
diff changeset
    35
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    36
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    37
The function `fetch_config(fetchattrs, mainattr=None)` simplifies the
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    38
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
    39
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
    40
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
    41
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
    42
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
    43
`cubicweb.entities`.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    44
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    45
For example: ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    46
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    47
  class Transition(AnyEntity):
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    48
    """..."""
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
    49
    __regid__ = 'Transition'
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    50
    fetch_attrs, fetch_order = fetch_config(['name'])
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    51
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    52
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
    53
the attribute `name` and sort by default on this attribute.