Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
24 changes: 21 additions & 3 deletions pynetbox/core/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,31 @@ 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)
...
test1-leaf1
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")
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down