Changes between Version 56 and Version 57 of S3/S3REST/s3_rest_controller


Ignore:
Timestamp:
02/15/11 11:30:01 (11 years ago)
Author:
Dominic König
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • S3/S3REST/s3_rest_controller

    v56 v57  
    310310== Advanced Options ==
    311311
     312=== Filtering Lists ===
     313
     314You 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
     323Note that {{{response.s3.filter}}} affects both, the primary resource and components!
     324
     325In {{{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
     338However, {{{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
     343This can be an unwanted side-effect.
     344
     345To 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
     358In this case, all {{{person}}} records would be selected - while only {{{address}}} records of type 1 would be retrieved.
     359
     360
    312361=== Pre-populating Create-Forms ===
    313362