Response
This page documents the classes that wrap responses returned by NetBox: Record (a single object) and RecordSet (a lazy collection of records).
Record Class
A Record represents a single object returned by the NetBox API. API fields are exposed as attributes; nested objects are recursively wrapped as their own Record instances. Records returned from a list endpoint are initially "shallow" — accessing an attribute not present in the list response causes pynetbox to fetch the full detail view on demand.
pynetbox.core.response.Record
Create Python objects from NetBox API responses.
Creates an object from a NetBox 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 attr is requested
(e.g. requesting a field that's only present on a full response on
a Record made from a nested response) then pynetbox will make a
request for the full object and return the requested value.
Examples
Default representation of the object is usually its name:
x = nb.dcim.devices.get(1)
x
# test1-switch1
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': {},
'role': {
'id': 1,
'name': 'Test Switch',
'slug': 'test-switch',
'url': 'http://localhost:8000/api/dcim/device-roles/1/'
},
'device_type': {...},
'display_name': 'test1-switch1',
'face': {'label': 'Rear', 'value': 1},
'id': 1,
'name': 'test1-switch1',
'parent_device': None,
'platform': {...},
'position': 1,
'primary_ip': {
'address': '192.0.2.1/24',
'family': 4,
'id': 1,
'url': 'http://localhost:8000/api/ipam/ip-addresses/1/'
},
'primary_ip4': {...},
'primary_ip6': None,
'rack': {
'display_name': 'Test Rack',
'id': 1,
'name': 'Test Rack',
'url': 'http://localhost:8000/api/dcim/racks/1/'
},
'site': {
'id': 1,
'name': 'TEST',
'slug': 'TEST',
'url': 'http://localhost:8000/api/dcim/sites/1/'
},
'status': {'label': 'Active', 'value': 1},
'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')
Source code in pynetbox/core/response.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 | |
delete()
Deletes an existing object.
:returns: True if DELETE operation was successful. :example:
x = nb.dcim.devices.get(name='test1-a3-tor1b') x.delete() True
Source code in pynetbox/core/response.py
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 | |
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: True
Source code in pynetbox/core/response.py
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 | |
save()
Saves changes to an existing object.
Takes a diff between the objects current state and its state at init and sends them as a dictionary to Request.patch().
:returns: True if PATCH request was successful. :example:
x = nb.dcim.devices.get(name='test1-a3-tor1b') x.serial '' x.serial = '1234' x.save() True
Source code in pynetbox/core/response.py
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 | |
serialize(nested=False, init=False)
Serializes an object
Pulls all the attributes in an object and creates a dict that can be turned into the json that netbox is expecting.
If an attribute's value is a Record type it's replaced with
the id field of that object.
When init=False (default), includes both original fields from the
API response and any fields that have been set on the object after
initialization. This allows proper change detection for fields set
to None or other values.
When init=True, returns only the original fields from the initial
API response, used for comparing against the current state to detect
changes.
.. note::
Using this to get a dictionary representation of the record
is discouraged. It's probably better to cast to dict()
instead. See Record docstring for example.
:returns: dict.
Source code in pynetbox/core/response.py
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 | |
update(data)
Update an object with a dictionary.
Accepts a dict 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().
:arg dict data: Dictionary containing the k/v to update the record object with. :returns: True if PATCH request was successful. :example:
x = nb.dcim.devices.get(1) x.update({ ... "name": "test-switch2", ... "serial": "ABC321", ... }) True
Source code in pynetbox/core/response.py
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 | |
updates()
Compiles changes for an existing object into a dict.
Takes a diff between the objects current state and its state at init and returns them as a dictionary, which will be empty if no changes.
:returns: dict. :example:
x = nb.dcim.devices.get(name='test1-a3-tor1b') x.serial '' x.serial = '1234' x.updates()
Source code in pynetbox/core/response.py
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 | |
RecordSet Class
A RecordSet is a one-shot iterator over Record objects, returned by Endpoint.all() and Endpoint.filter(). It pages through results from NetBox on demand. To iterate the results more than once, materialize the set with list().
pynetbox.core.response.RecordSet
Iterator containing Record objects.
Returned by Endpoint.all() and Endpoint.filter() methods.
Allows iteration of and actions to be taken on the results from the aforementioned
methods. Contains Record objects.
Examples
To see how many results are in a query by calling len():
x = nb.dcim.devices.all()
len(x)
# 123
Simple iteration of the results:
devices = nb.dcim.devices.all()
for device in devices:
print(device.name)
# test1-leaf1
# test1-leaf2
# test1-leaf3
Source code in pynetbox/core/response.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
delete()
Bulk deletes objects in a RecordSet.
Allows for batch deletion of multiple objects in a RecordSet.
Returns
True if bulk DELETE operation was successful.
Examples
Deleting offline devices on site 1:
netbox.dcim.devices.filter(site_id=1, status="offline").delete()
Source code in pynetbox/core/response.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
update(**kwargs)
Updates kwargs onto all Records in the RecordSet and saves these.
Updates are only sent to the API if a value were changed, and only for the Records which were changed.
Returns
True if the update succeeded, None if no update were required.
Examples
result = nb.dcim.devices.filter(site_id=1).update(status='active')
# True
Source code in pynetbox/core/response.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |