Changes between Version 25 and Version 26 of S3/S3Model/SuperEntities


Ignore:
Timestamp:
03/03/14 10:45:45 (8 years ago)
Author:
Dominic König
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • S3/S3Model/SuperEntities

    v25 v26  
    2121=== Defining a Super-Entity ===
    2222
    23 To define a super-entity, you can use the {{{super_entity()}}} function:
     23A super-entity can be defined using the {{{super_entity()}}} method of S3Model:
    2424
    25 {{{
    26 table = s3db.super_entity(tablename, "sit_id", situation_types,
    27                           Field("datetime", "datetime"), # shared field
    28                           location_id())                 # shared field
     25{{{#!python
     26class MyModel(S3Model):
     27
     28    def model(self):
     29
     30        # Define a dict with nice names for the instance types:
     31        my_instance_types = {
     32            "my_instance_tablename": T("Nice name for the instance type"),
     33        }
     34
     35        # Define the super-entity
     36        tablename = "my_super_entity"
     37        table = self.super_entity(tablename,
     38                                  "my_super_id",                   # specify the super ID
     39                                  my_instance_types,               # specify the instance types
     40                                  Field("start_date", "datetime"), # shared field
     41                                  location_id())                   # shared field
    2942
    3043}}}
    3144
    32 You can define so-called ''shared fields'' in the super-entity, which are mirrored from the respective resource record. This allows to easily access these data from the component without the need to involve the primay instance table in the backward-join:
     45Shared fields will mirror the values from the respective instance record, which allows easy access without need to join multiple tables.
    3346
    34 In case the names of the shared fields in your instance table differ from those of the super-entity, you can define a field mapping:
     47If the names of shared fields are different in certain instance tables, you can define a field mapping when you configure the instance table:
    3548
     49{{{#!python
     50
     51        # Define the instance table
     52        tablename = "my_instance_table"
     53        table = self.define_table(tablename,
     54                                  Field("first_date", "datetime"),
     55                                  Field("last_date", "datetime"),
     56                                  *s3_meta_fields())
     57
     58        # Configure the instance table
     59        self.configure("my_instance_table",
     60                       # Configure the super entity
     61                       super_entity = "my_super_entity",
     62                       # Map the "first_date" field in my_instance_table to "start_date" in my_super_entity
     63                       my_super_entity_fields = {"start_date": "first_date"},
     64                  )
     65
     66}}}
    3667=== Defining an Instance of a Super-Entity ===
    3768