| | 312 | === Filtering Lists === |
| | 313 | |
| | 314 | You can filter lists by setting {{{response.s3.filter}}} to a filter query: |
| | 315 | |
| | 316 | {{{ |
| | 317 | # This filters for females: |
| | 318 | response.s3.filter = (db.pr_person.gender == 2) |
| | 319 | |
| | 320 | return s3_rest_controller("pr", "person") |
| | 321 | }}} |
| | 322 | |
| | 323 | Note that {{{response.s3.filter}}} affects both, the primary resource and components! |
| | 324 | |
| | 325 | In {{{prep}}}, you can also add filter queries using the {{{add_filter}}} method: |
| | 326 | |
| | 327 | {{{ |
| | 328 | def prep(r): |
| | 329 | resource = r.resource |
| | 330 | query = (db.pr_address.type==1) # Home addresses only |
| | 331 | resource.add_filter(query) |
| | 332 | return True |
| | 333 | response.s3.prep = prep |
| | 334 | |
| | 335 | return s3_rest_controller("pr", "person") |
| | 336 | }}} |
| | 337 | |
| | 338 | However, {{{add_filter}}} again affects both, primary and component records - so this example would: |
| | 339 | |
| | 340 | - only retrieve {{{person}}} records which have a type 1 {{{address}}} record |
| | 341 | - only retrieve the {{{address}}} records with type 1. |
| | 342 | |
| | 343 | This can be an unwanted side-effect. |
| | 344 | |
| | 345 | To have the primary resource unfiltered, and filter only records in a particular component, you can use {{{add_component_filter}}}: |
| | 346 | |
| | 347 | {{{ |
| | 348 | def prep(r): |
| | 349 | resource = r.resource |
| | 350 | query = (db.pr_address.type==1) # Home addresses only |
| | 351 | resource.add_component_filter("address", query) |
| | 352 | return True |
| | 353 | response.s3.prep = prep |
| | 354 | |
| | 355 | return s3_rest_controller("pr", "person") |
| | 356 | }}} |
| | 357 | |
| | 358 | In this case, all {{{person}}} records would be selected - while only {{{address}}} records of type 1 would be retrieved. |
| | 359 | |
| | 360 | |