Changes between Version 7 and Version 8 of S3/DynamicTables


Ignore:
Timestamp:
02/21/17 10:40:43 (8 years ago)
Author:
Dominic König
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • S3/DynamicTables

    v7 v8  
    148148== Dynamic Components ==
    149149
    150    ''tbw''
     150=== Component key definition ===
     151
     152Dynamic tables can be components of static tables.
     153
     154To achieve this, the dynamic model must have a foreign key field that links to the static table, e.g.:
     155
     156{{{#!python
     157
     158s3db = current.s3db
     159
     160ttable = s3db.s3_table
     161ftable = s3db.s3_field
     162
     163# Create dynamic table
     164table_id = ttable.insert(name = "s3dt_example,
     165                         )
     166
     167# The dynamic data model
     168model = (
     169    # Component key
     170    {"name": "organisation_id",
     171     "field_type": "reference org_organisation",
     172
     173     # Indicate that this field is the foreign key that links to the master table (including component alias):
     174     "component_key": True,
     175     "component_alias": "rating",
     176
     177     },
     178    # Other field
     179    {"name": "value",
     180     "field_type": "integer",
     181     "options": {1: "very good",
     182                 2: "good",
     183                 3: "average",
     184                 4: "acceptable",
     185                 5: "poor",
     186                 },
     187     },
     188    )
     189
     190for field_def in model:
     191
     192    # Add field definition
     193    record = Storage(field_def)
     194    record["table_id"] = table_id
     195    record_id = ftable.insert(**record)
     196
     197    # Run onaccept (required!)
     198    record["id"] = record_id
     199    s3db.onaccept(ftable, record)
     200
     201}}}
     202
     203Further, dynamic components must be enabled for the master table (they are disabled by default for performance reasons):
     204{{{#!python
     205s3db.configure("org_organisation", dynamic_components=True)
     206}}}
     207
     208With the settings, the dynamic component can be accessed from the master table like this:
     209{{{
     210# Access the dynamic table as component
     211GET http://localhost:8000/eden/org/organisation/8/rating
     212}}}