| | 109 | S3QueryField instances can also be generated at the back-end, simply by: |
| | 110 | |
| | 111 | {{{ |
| | 112 | selector = S3QueryField(<field selector>) |
| | 113 | }}} |
| | 114 | |
| | 115 | Such selectors can then be used to create filter queries, using a syntax very similar to web2py queries: |
| | 116 | |
| | 117 | {{{ |
| | 118 | afilter = S3QueryField("person.first_name") == "Dominic" |
| | 119 | bfilter = ~(S3QueryField("person.first_name").like("Dominic")) # negation by ~ operator |
| | 120 | }}} |
| | 121 | |
| | 122 | The equivalent query operators are: |
| | 123 | |
| | 124 | ||'''Python Operator'''||'''URL Operator'''|| |
| | 125 | ||{{{<}}}||!__lt|| |
| | 126 | ||{{{<=}}}||!__le|| |
| | 127 | ||{{{==}}}||!__eq|| |
| | 128 | ||{{{!=}}}||!__ne|| |
| | 129 | ||{{{>=}}}||!__ge|| |
| | 130 | ||{{{>}}}||!__gt|| |
| | 131 | ||{{{.like()}}}||!__like|| |
| | 132 | ||{{{.contains()}}}||!__contains|| |
| | 133 | ||{{{.belongs()}}}||!__belongs|| |
| | 134 | |
| | 135 | These filter queries can also be joined together by {{{&}}} and {{{|}}} operators: |
| | 136 | |
| | 137 | {{{ |
| | 138 | cfilter = (S3QueryField("person.first_name") == "Dominic") & (S3QueryField("contact.value") == "dominic@nursix.org") |
| | 139 | }}} |
| | 140 | |
| | 141 | Note: be diligent with brackets here ({{{&}}} and {{{|}}} operators take precedence). |
| | 142 | |
| | 143 | To add a filter to a resource is as simple as: |
| | 144 | |
| | 145 | {{{ |
| | 146 | myfilter = S3QueryField("person.first_name") == "Dominic" |
| | 147 | resource.add_filter(myfilter) |
| | 148 | }}} |
| | 149 | |
| | 150 | Note that filters (S3ResourceQuery instances) are resource-agnostic, so they can be re-used across resources (provided the same field selector applies in all of them). |
| | 151 | |
| | 152 | Filters for fields which are not defined in a resource will be ignored quietly when retrieving records from the resource. |