Skip to content

Commit 046d594

Browse files
author
Zach Moody
committed
Fixes #127 - Record iter doesn't handle list of records
Fixes __iter__ method on Record object to properly return lists of other record objects. Like tagged_vlans on Interface objects.
1 parent 0547b3c commit 046d594

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

pynetbox/core/response.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ def __iter__(self):
183183
cur_attr = getattr(self, i)
184184
if isinstance(cur_attr, Record):
185185
yield i, dict(cur_attr)
186+
elif isinstance(cur_attr, list) and isinstance(
187+
cur_attr[0], Record
188+
):
189+
yield i, [dict(x) for x in cur_attr]
186190
else:
187191
yield i, cur_attr
188192

tests/unit/test_response.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,56 @@ def test_diff(self):
7777
test.nested_dict = 1
7878
test.string_field = 'foobaz'
7979
self.assertEqual(test._diff(), {'tags', 'nested_dict', 'string_field'})
80+
81+
def test_dict(self):
82+
test_values = {
83+
'id': 123,
84+
'custom_fields': {
85+
'foo': 'bar'
86+
},
87+
'string_field': 'foobar',
88+
'int_field': 1,
89+
"nested_dict": {
90+
"id": 222,
91+
"name": 'bar',
92+
},
93+
'tags': [
94+
'foo',
95+
'bar',
96+
],
97+
'int_list': [
98+
123,
99+
321,
100+
231,
101+
],
102+
'record_list': [
103+
{
104+
'id': 123,
105+
'name': 'Test',
106+
'str_attr': 'foo',
107+
'int_attr': 123,
108+
'custom_fields': {
109+
'foo': 'bar'
110+
},
111+
'tags': [
112+
'foo',
113+
'bar',
114+
],
115+
},
116+
{
117+
'id': 321,
118+
'name': 'Test 1',
119+
'str_attr': 'bar',
120+
'int_attr': 321,
121+
'custom_fields': {
122+
'foo': 'bar'
123+
},
124+
'tags': [
125+
'foo',
126+
'bar',
127+
],
128+
},
129+
]
130+
}
131+
test = Record(test_values, None, None)
132+
self.assertEqual(dict(test), test_values)

0 commit comments

Comments
 (0)