Skip to content

Response

pynautobot.core.response

Provides classes and functions to handle responses from the Nautobot API.

pynautobot.core.response.JsonField

Explicit field type for values that are not to be converted to a Record object.

pynautobot.core.response.Record

Create Python objects from Nautobot API responses.

Creates an object from a Nautobot response passed as values. Nested dicts that represent other endpoints are also turned into Record objects. All fields are then assigned to the object's attributes. If a missing attribute is requested (e.g., requesting a field that's only present on a full response on a Record made from a nested response), the pynautobot will make a request for the full object and return the requested value.

Examples:

Default representation of the object usually contains object's class, object's name, and its location in memory

>>> x = nb.dcim.devices.get(1)
>>> x
<pynautobot.models.dcim.Devices ('test1-switch1') at 0x1953d821250>
>>>

Querying a string field.

>>> x = nb.dcim.devices.get(1)
>>> x.serial
'ABC123'
>>>

Querying a field on a nested object.

>>> x = nb.dcim.devices.get(1)
>>> x.device_type.model
'QFX5100-24Q'
>>>

Casting the object as a dictionary.

>>> from pprint import pprint
>>> pprint(dict(x))
{'asset_tag': None,
 'cluster': None,
 'comments': '',
 'config_context': {},
 'created': '2018-04-01',
 'custom_fields': {},
 'device_role': {...},
 'device_type': {...},
 'display_name': 'test1-switch1',
 'face': {'label': 'Rear', 'value': 1},
 'id': 1,
 'name': 'test1-switch1',
 'parent_device': None,
 'platform': {...},
 'position': 1,
 'primary_ip': {...},
 'primary_ip4': {...},
 'primary_ip6': None,
 'rack': {...},
 'serial': 'ABC123',
 'site': {...},
 'status': {...},
 'tags': [],
 'tenant': None,
 'vc_position': None,
 'vc_priority': None,
 'virtual_chassis': None}

Iterating over a Record object.

>>> for i in x:
...     print(i)
...
('id', 1)
('name', 'test1-switch1')
('display_name', 'test1-switch1')
>>>

notes property

Represents the notes detail endpoint.

Returns a list of DetailEndpoint objects that are related to the passed in object

:returns: :py:class:.DetailEndpoint

:Examples:

Notes associated to a device object:

device = nb.dcim.devices.get(name="test") device.notes.list() [<pynautobot.core.response.Record ('test - 2024-07-16T11:59:00.169296+00:00')...]

Notes associated to a controller object:

controller = nb.dcim.controllers.get(name="test") controller.notes.list() [<pynautobot.core.response.Record ('test - 2024-07-16T11:59:00.169296+00:00')...]

Create new note on device object:

device = nb.dcim.devices.get(name="test") device.notes.create({"note": "foo bar"}) [<pynautobot.core.response.Record ('test - 2024-07-16T18:45:07.653263+00:00')...]

__eq__(other)

Check if the Record object is equal to another object.

__getattr__(k)

Default behavior for missing attributes.

We'll call full_details() if we're asked for an attribute we don't have.

Parameters:

Name Type Description Default
k str

The name of the attribute.

required

Returns:

Type Description
Any

The value of the requested attribute.

Raises:

Type Description
AttributeError

If the attribute is not found.

Note

In order to prevent non-explicit behavior, k='keys' is excluded because casting to dict() calls this attribute.

__getitem__(k)

Get an item from the Record object.

__getstate__()

Get the state of the Record object.

__hash__()

Hash the Record object.

__init__(values, api, endpoint)

Initialize the Record object.

__iter__()

Iterate over the Record object.

__key__()

Get the key of the Record object.

__repr__()

Return the representation of the Record object.

__setstate__(d)

Set the state of the Record object.

__str__()

Return the string representation of the Record object.

delete()

Deletes an existing object.

Returns:

Type Description
bool

True if the DELETE operation was successful.

Examples:

>>> x = nb.dcim.devices.get(name='test1-a3-tor1b')
>>> x.delete()
True

full_details()

Queries the hyperlinked endpoint if 'url' is defined.

This method will populate the attributes from the detail endpoint when it's called. Sets the class-level has_details attribute when it's called to prevent being called more than once.

Returns: (bool)

save()

Saves changes to an existing object.

Takes a diff between the object's current state and its state at initialization and sends them as a dictionary to Request.patch().

Returns:

Type Description
bool

True if the PATCH request was successful.

Examples:

>>> x = nb.dcim.devices.get(name='test1-a3-tor1b')
>>> x.serial
''
>>> x.serial = '1234'
>>> x.save()
True

serialize(nested=False, init=False)

Serializes the object.

Pulls all the attributes in an object and creates a dictionary that can be turned into the JSON format expected by Nautobot.

Parameters:

Name Type Description Default
nested bool

Whether to include nested objects in the serialization. Default is False.

False
init bool

Whether to include initialization attributes in the serialization. Default is False.

False

Returns:

Type Description
dict

A dictionary representation of the serialized object.

Note

Using this method to get a dictionary representation of the record is discouraged. It's probably better to cast to dict() instead. See the Record docstring for an example.

update(data)

Update an object with a dictionary.

Accepts a dictionary and uses it to update the record and call save(). For nested and choice fields you'd pass an int the same as if you were modifying the attribute and calling save().

Parameters:

Name Type Description Default
data dict

Dictionary containing the key-value pairs to update the record object with.

required

Returns:

Type Description
bool

True if the PATCH request was successful.

Examples:

>>> x = nb.dcim.devices.get(1)
>>> x.update({
...   "name": "test-switch2",
...   "serial": "ABC321",
... })
True

updates()

Compiles changes for an existing object into a dictionary.

Takes a diff between the object's current state and its state at initialization and returns them as a dictionary. Returns an empty dictionary if no changes have been made.

Returns:

Type Description
dict

A dictionary containing the changes made to the object.

Examples:

>>> x = nb.dcim.devices.get(name='test1234')
>>> x.serial
''
>>> x.serial = '1234'
>>> x.updates()
{'serial': '1234'}

pynautobot.core.response.get_return(lookup, return_fields=None)

Returns simple representations for items passed to lookup.

Parameters:

Name Type Description Default
lookup dict | Record

The object or collection to retrieve representations for.

required
return_fields list

A list of fields to reference when calling values on lookup.

None
Note

Used to return a "simple" representation of objects and collections sent to it via lookup. Otherwise, we look to see if lookup is a "choices" field (dict with only 'id' and 'value') or a nested_return. Finally, we check if it's a Record, if so simply return a string. Order is important due to nested_return being self-referential.