diff --git a/README.md b/README.md index 920747d6..67e3062b 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,11 @@ test1-leaf3 >>> ``` +Note that the all() and filter() methods are generators and return an object that can be iterated over only once. If you are going to be iterating over it repeatedly you need to either call the all() method again, or encapsulate the results in a `list` object like this: +``` +>>> devices = list(nb.dcim.devices.all()) +``` + ### Threading pynetbox supports multithreaded calls (in Python 3 only) for `.filter()` and `.all()` queries. It is **highly recommended** you have `MAX_PAGE_SIZE` in your Netbox install set to anything *except* `0` or `None`. The default value of `1000` is usually a good value to use. To enable threading, add `threading=True` parameter to the `.api`: diff --git a/pynetbox/core/endpoint.py b/pynetbox/core/endpoint.py index fb0163b9..804f161a 100644 --- a/pynetbox/core/endpoint.py +++ b/pynetbox/core/endpoint.py @@ -79,14 +79,16 @@ def all(self, limit=0, offset=None): Returns all objects from an endpoint. :arg int,optional limit: Overrides the max page size on - paginated returns. + paginated returns. This defines the number of records that will + be returned with each query to the Netbox server. The queries + will be made as you iterate through the result set. :arg int,optional offset: Overrides the offset on paginated returns. :Returns: A :py:class:`.RecordSet` object. :Examples: - >>> devices = nb.dcim.devices.all() + >>> devices = list(nb.dcim.devices.all()) >>> for device in devices: ... print(device.name) ... @@ -94,6 +96,14 @@ def all(self, limit=0, offset=None): test1-leaf2 test1-leaf3 >>> + + If you want to iterate over the results multiple times then + encapsulate them in a list like this: + >>> devices = list(nb.dcim.devices.all()) + + This will cause the entire result set + to be fetched from the server. + """ if limit == 0 and offset is not None: raise ValueError("offset requires a positive limit value") @@ -185,7 +195,9 @@ def filter(self, *args, **kwargs): :arg str,optional \**kwargs: Any search argument the endpoint accepts can be added as a keyword arg. :arg int,optional limit: Overrides the max page size on - paginated returns. + paginated returns. This defines the number of records that will + be returned with each query to the Netbox server. The queries + will be made as you iterate through the result set. :arg int,optional offset: Overrides the offset on paginated returns. :Returns: A :py:class:`.RecordSet` object. @@ -233,6 +245,12 @@ def filter(self, *args, **kwargs): test1-a3-spine2 test1-a3-leaf1 >>> + + To have the ability to iterate over the results multiple times then + encapsulate them in a list. This will cause the entire result set + to be fetched from the server. + + >>> devices = list(nb.dcim.devices.filter(role='leaf-switch')) """ if args: