150 | | ''tbw'' |
| 150 | === Component key definition === |
| 151 | |
| 152 | Dynamic tables can be components of static tables. |
| 153 | |
| 154 | To achieve this, the dynamic model must have a foreign key field that links to the static table, e.g.: |
| 155 | |
| 156 | {{{#!python |
| 157 | |
| 158 | s3db = current.s3db |
| 159 | |
| 160 | ttable = s3db.s3_table |
| 161 | ftable = s3db.s3_field |
| 162 | |
| 163 | # Create dynamic table |
| 164 | table_id = ttable.insert(name = "s3dt_example, |
| 165 | ) |
| 166 | |
| 167 | # The dynamic data model |
| 168 | model = ( |
| 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 | |
| 190 | for 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 | |
| 203 | Further, dynamic components must be enabled for the master table (they are disabled by default for performance reasons): |
| 204 | {{{#!python |
| 205 | s3db.configure("org_organisation", dynamic_components=True) |
| 206 | }}} |
| 207 | |
| 208 | With the settings, the dynamic component can be accessed from the master table like this: |
| 209 | {{{ |
| 210 | # Access the dynamic table as component |
| 211 | GET http://localhost:8000/eden/org/organisation/8/rating |
| 212 | }}} |