doc/book/en/development/entityclasses/data-as-objects.rst
branchstable
changeset 5394 105011657405
parent 5393 875bdc0fe8ce
child 5395 e0ab7433e640
equal deleted inserted replaced
5393:875bdc0fe8ce 5394:105011657405
     1 Access to persistent data
       
     2 --------------------------
       
     3 
       
     4 Python-level access to persistent data is provided by the
       
     5 :class:`Entity <cubicweb.entity>` class.
       
     6 
       
     7 An entity class is bound to a schema entity type.  Descriptors are added when
       
     8 classes are registered in order to initialize the class according to its schema:
       
     9 
       
    10 * we can access the defined attributes in the schema thanks to the attributes of
       
    11   the same name on instances (typed value)
       
    12 
       
    13 * we can access the defined relations in the schema thanks to the relations of
       
    14   the same name on instances (entities instances list)
       
    15 
       
    16 
       
    17 :Formatting and output generation:
       
    18 
       
    19   * `view(__vid, __registry='views', **kwargs)`, applies the given view to the entity
       
    20     (and returns an unicode string)
       
    21 
       
    22   * `absolute_url(*args, **kwargs)`, returns an absolute URL to access the primary view
       
    23     of an entity
       
    24 
       
    25   * `rest_path()`, returns a relative REST URL to get the entity
       
    26 
       
    27   * `printable_value(attr, value=_marker, attrtype=None, format='text/html', displaytime=True)`,
       
    28     returns a string enabling the display of an attribute value in a given format
       
    29     (the value is automatically recovered if necessary)
       
    30 
       
    31 :Data handling:
       
    32 
       
    33   * `as_rset()`, converts the entity into an equivalent result set simulating the
       
    34      request `Any X WHERE X eid _eid_`
       
    35 
       
    36   * `complete(skip_bytes=True)`, executes a request that recovers at
       
    37     once all the missing attributes of an entity
       
    38 
       
    39   * `get_value(name)`, returns the value associated to the attribute name given
       
    40     in parameter
       
    41 
       
    42   * `related(rtype, role='subject', limit=None, entities=False)`,
       
    43     returns a list of entities related to the current entity by the
       
    44     relation given in parameter
       
    45 
       
    46   * `unrelated(rtype, targettype, role='subject', limit=None)`,
       
    47     returns a result set corresponding to the entities not (yet)
       
    48     related to the current entity by the relation given in parameter
       
    49     and satisfying its constraints
       
    50 
       
    51   * `set_attributes(**kwargs)`, updates the attributes list with the corresponding
       
    52     values given named parameters
       
    53 
       
    54   * `set_relations(**kwargs)`, add relations to the given object. To
       
    55      set a relation where this entity is the object of the relation,
       
    56      use `reverse_<relation>` as argument name.  Values may be an
       
    57      entity, a list of entities, or None (meaning that all relations of
       
    58      the given type from or to this object should be deleted).
       
    59 
       
    60   * `copy_relations(ceid)`, copies the relations of the entities having the eid
       
    61     given in the parameters on the current entity
       
    62 
       
    63   * `delete()` allows to delete the entity
       
    64 
       
    65 
       
    66 The :class:`AnyEntity` class
       
    67 ----------------------------
       
    68 
       
    69 To provide a specific behavior for each entity, we have to define a class
       
    70 inheriting from `cubicweb.entities.AnyEntity`. In general, we define this class
       
    71 in `mycube.entities` module (or in a submodule if we want to split code among
       
    72 multiple files) so that it will be available on both server and client side.
       
    73 
       
    74 The class `AnyEntity` is a sub-class of Entity that add methods to it,
       
    75 and helps specializing (by further subclassing) the handling of a
       
    76 given entity type.
       
    77 
       
    78 Most methods defined for `AnyEntity`, in addition to `Entity`, add
       
    79 support for the `Dublin Core`_ metadata.
       
    80 
       
    81 .. _`Dublin Core`: http://dublincore.org/
       
    82 
       
    83 :Standard meta-data (Dublin Core):
       
    84 
       
    85   * `dc_title()`, returns a unicode string corresponding to the
       
    86     meta-data `Title` (used by default is the first non-meta attribute
       
    87     of the entity schema)
       
    88 
       
    89   * `dc_long_title()`, same as dc_title but can return a more
       
    90     detailed title
       
    91 
       
    92   * `dc_description(format='text/plain')`, returns a unicode string
       
    93     corresponding to the meta-data `Description` (looks for a
       
    94     description attribute by default)
       
    95 
       
    96   * `dc_authors()`, returns a unicode string corresponding to the meta-data
       
    97     `Authors` (owners by default)
       
    98 
       
    99   * `dc_creator()`, returns a unicode string corresponding to the
       
   100     creator of the entity
       
   101 
       
   102   * `dc_date(date_format=None)`, returns a unicode string corresponding to
       
   103     the meta-data `Date` (update date by default)
       
   104 
       
   105   * `dc_type(form='')`, returns a string to display the entity type by
       
   106     specifying the preferred form (`plural` for a plural form)
       
   107 
       
   108   * `dc_language()`, returns the language used by the entity
       
   109 
       
   110 
       
   111 :Misc methods:
       
   112 
       
   113   * `after_deletion_path`, return (path, parameters) which should be
       
   114      used as redirect information when this entity is being deleted
       
   115 
       
   116   * `pre_web_edit`, callback called by the web editcontroller when an
       
   117     entity will be created/modified, to let a chance to do some entity
       
   118     specific stuff (does nothing by default)
       
   119 
       
   120 Inheritance
       
   121 -----------
       
   122 
       
   123 When describing a data model, entities can inherit from other entities as is
       
   124 common in object-oriented programming.
       
   125 
       
   126 You have the possibility to adapt some entity attributes, as follow:
       
   127 
       
   128 .. sourcecode:: python
       
   129 
       
   130     from cubes.OTHER_CUBE import entities
       
   131     class EntityExample(entities.EntityExample):
       
   132         def dc_long_title(self):
       
   133             return '%s (%s)' % (self.name, self.description)
       
   134 
       
   135 Notice this is different than yams schema inheritance.
       
   136