Filtering Collections¶
Collections returned by the LinodeClient
can be filtered using a
SQLAlchemy-like syntax. When calling any “get” method of the LinodeClient
class of one of its groups, any number of filters may be passed in as boolean
comparisons between attributes of the model returned by the collection.
When filtering on API responses for list endpoints, you will first need
to import the corresponding object class.
For example, to filter on instances you must first Import Instance
:
from linode_api4 import Instance
For example, calling instances
returns a list of Instance
objects, so we can use properties of Instance
to filter the results:
# returns all Instances in the "prod" group
client.linode.instances(Instance.group == "prod")
You can use any boolean comparisons when filtering collections:
# returns all Instances _not_ in us-east-1a
client.linode.instances(Instance.region != "us-east-1a")
You can combine filters to be even more specific - by default all filters are considered:
# returns all Instances in the "prod" group that are in us-east-1a
client.linode.instances(Instance.group == "prod",
Instance.region == "us-east-1a")
If you need to combine the results of two filters, you can use or_
to define
this relationship:
# returns all Instance in either the "prod" or "staging" groups
client.linode.instances(or_(Instance.group == "prod",
Instance.group == "staging"))
and_
is also available in case you need to do deeply-nested comparisons:
# returns all Instances in the group "staging" and any Instances in the "prod"
# group that are located in "us-east-1a"
client.linode.instances(or_(Instance.group == "staging",
and_(Instance.group == "prod",
Instance.region == "us-east-1a"))
-
class
linode_api4.objects.filtering.
Filter
(dct)[source]¶ A Filter represents a comparison to send to the API. These should not be constructed normally, but instead should be returned from comparisons between class attributes of filterable classes (see above). Filters can be combined with
and_
andor_
.
-
linode_api4.objects.filtering.
and_
(a, b)[source]¶ Combines two
Filters
with an “and” operation, matching any results that match both of the given filters.Parameters: Returns: A filter that matches both a and b
Return type:
-
linode_api4.objects.filtering.
limit
(amount)[source]¶ Allows limiting of results in a collection. You may only ever apply a limit once per request. For example:
# returns my first 5 Instances client.linode.instances(limit(5))
Parameters: amount (int) – The number of results to return. Returns: A filter that will limit the number of results returned. Return type: Filter
-
linode_api4.objects.filtering.
or_
(a, b)[source]¶ Combines two
Filters
with an “or” operation, matching any results that match any of the given filters.Parameters: Returns: A filter that matches either a or b
Return type:
-
linode_api4.objects.filtering.
order_by
(field, desc=False)[source]¶ Allows ordering of results. You may only ever order a collection’s results once in a given request. For example:
# sort results by Instances group client.linode.instances(order_by(Instance.group))
Parameters: - field (FilterableAttribute) – The field to order results by. Must be a filterable attribute of the model.
- desc (bool) – If True, return results in descending order. Defaults to False
Returns: A filter that will order results as requested.
Return type: