Changes between Version 29 and Version 30 of S3/S3Model/SuperEntities


Ignore:
Timestamp:
03/03/14 10:56:04 (11 years ago)
Author:
Dominic König
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • S3/S3Model/SuperEntities

    v29 v30  
    4646=== Defining an Instance of a Super-Entity ===
    4747
    48 To make a table an instance of a super-entity, you can use {{{s3db.configure()}}}:
     48A table is declared an instance of a super-entity by configuring the {{{super_entity}}} hook (continuing the example from above):
    4949
    50 {{{
    51 s3db.configure(table, super_entity = db.sit_situation)
     50{{{#!python
     51
     52        # Define the instance table
     53        tablename = "my_instance_table"
     54        table = self.define_table(tablename,
     55                                  # Instance table must have the super ID:
     56                                  self.super_link("my_super_id", "my_super_entity"),
     57                                  Field("start_date", "datetime"),
     58                                  Field("end_date", "datetime"),
     59                                  *s3_meta_fields())
     60
     61        # Configure the instance table
     62        self.configure("my_instance_table",
     63
     64                       # Configure the super entity
     65                       super_entity = "my_super_entity",
     66                      )
     67
    5268}}}
    5369
    54 By default, all fields that the table and the super-entity have in common will be mirrored in the super-entity ("shared fields"). You can override this by specifying a list of fields to be mirrored by the super-entity.
     70By default, all fields which have the same name in super-entity and instance table will become shared fields.
    5571
    56 {{{
    57 s3db.configure(table,
    58                super_entity = db.sit_situation,
    59                sit_situation_fields = ["datetime"])
    60 }}}
     72If only a subset of these fields shall be shared, or the shared fields have different names in super entity and instance table, you can define a field mapping like this:
    6173
    62 In case your table uses different names for the shared fields, you can use a dict instead to specify a mapping:
     74{{{#!python
    6375
    64 {{{
    65 s3db.configure(table,
    66                super_entity = db.sit_situation,
    67                sit_situation_fields = dict(datetime="timestmp", location_id="location_id"))
     76        # Define the instance table
     77        tablename = "my_instance_table"
     78        table = self.define_table(tablename,
     79                                  # Instance table must have the super ID:
     80                                  self.super_link("my_super_id", "my_super_entity"),
     81                                  Field("first_date", "datetime"),
     82                                  Field("last_date", "datetime"),
     83                                  *s3_meta_fields())
     84
     85        # Configure the instance table
     86        self.configure("my_instance_table",
     87
     88                       # Configure the super entity
     89                       super_entity = "my_super_entity",
     90
     91                       # Map the "first_date" field in my_instance_table to "start_date" in my_super_entity
     92                       my_super_entity_fields = {"start_date": "first_date"},
     93                  )
     94
    6895}}}
    6996=== Linking to a Super-Entity ===