Changes between Version 25 and Version 26 of S3/S3Model/SuperEntities
- Timestamp:
- 03/03/14 10:45:45 (11 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
S3/S3Model/SuperEntities
v25 v26 21 21 === Defining a Super-Entity === 22 22 23 To define a super-entity, you can use the {{{super_entity()}}} function:23 A super-entity can be defined using the {{{super_entity()}}} method of S3Model: 24 24 25 {{{ 26 table = s3db.super_entity(tablename, "sit_id", situation_types, 27 Field("datetime", "datetime"), # shared field 28 location_id()) # shared field 25 {{{#!python 26 class 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 29 42 30 43 }}} 31 44 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: 45 Shared fields will mirror the values from the respective instance record, which allows easy access without need to join multiple tables. 33 46 34 I n case the names of the shared fields in your instance table differ from those of the super-entity, you can define a field mapping:47 If the names of shared fields are different in certain instance tables, you can define a field mapping when you configure the instance table: 35 48 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 }}} 36 67 === Defining an Instance of a Super-Entity === 37 68