Filtering Results Using Operators

You can filter a collection using various operators. For example, to find the users that contain a specific text:

http://<hostname or IP address>/api/v2/<multi_result_entity>/?<field_name>__contains=<text>

In the previous example, <hostname or IP address> is the IP address of your Oracle Linux Automation Manager. <multi_result_entity> is an entity that includes more than one result set, such as, for example, where there are more than one user instance for the users entity. <field_name> is the name of the field to apply the search text to, and <text> is the name of the text to search.

To find a specific match, you can use the following syntax:

http://<hostname or IP address>/api/v2/<multi_result_entity>/?<field_name>=<text>

For integer type rsources, add \_\_int to the end to send the string input value as an integer. For example,

http://<hostname or IP address>/api/v2/<multi_result_entity>/?<field_name>__int=<integer>

You can also filter for related resources. For example,

http://<hostname or IP address>/api/v2/<multi_result_entity>/?<field_name>__icontains=<text>

All resources with a field that includes the text are returned.

You can also filter with multiple fields. For example,

http://<hostname or IP address>/api/v2/<multi_result_entity>/?<field_name>__icontains=<text>&<field_name>=<text>

For more about available operators types, see the following Web site: https://docs.djangoproject.com/en/dev/ref/models/querysets/

You can use additional query string parameters to filter the list of results returned to those matching a given value. Only fields and relations that exist in the database may be used for filtering. Any special characters in the specified value should be url-encoded. For example:

?field=value%20xyz

Fields may also span relations, only for fields and relationships defined in the database:

?other__field=value

To exclude results matching certain criteria, prefix the field parameter with not__:

?not__field=value

By default, all query string filters are joined using the AND operator, so only the results matching all filters are returned. To combine results matching any one of multiple criteria, prefix each query string parameter with or__:

?or__field=value&or__field=othervalue
?or__not__field=value&or__field=othervalue

The default AND filtering applies all filters simultaneously to each related object being filtered across database relationships. The chain filter instead applies filters separately for each related object. To use, prefix the query string parameter with chain__:

?chain__related__field=value&chain__related__field2=othervalue
?chain__not__related__field=value&chain__related__field2=othervalue

If the first query above were written as ?related__field=value&related__field2=othervalue, it would return only the primary objects where the same related object satisfied both conditions. As written using the chain filter, it would return the intersection of primary objects matching each condition.

Field lookups may also be used for more advanced queries, by appending the lookup to the field name:

?field__lookup=value

The following field lookups are supported:

  • exact: Exact match (default lookup if not specified).

  • iexact: Case-insensitive version of exact.

  • contains: Field contains value.

  • icontains: Case-insensitive version of contains.

  • startswith: Field starts with value.

  • istartswith: Case-insensitive version of startswith.

  • endswith: Field ends with value.

  • iendswith: Case-insensitive version of endswith.

  • regex: Field matches the given regular expression.

  • iregex: Case-insensitive version of regex.

  • gt: Greater than comparison.

  • gte: Greater than or equal to comparison.

  • lt: Less than comparison.

  • lte: Less than or equal to comparison.

  • isnull: Check whether the given field or related object is null; expects a boolean value.

  • in: Check whether the given field's value is present in the list provided; expects a list of items.

  • Boolean values may be specified as True or 1 for true, False or 0 for false (both case-insensitive).

Null values may be specified as None or Null (both case-insensitive), though it is preferred to use the isnull lookup to explicitly check for null values.

Lists (for the in lookup) may be specified as a comma-separated list of values.

Filtering based on the requesting user's level of access by query string parameter:

  • role_level: Level of role to filter on, such as admin_role