File tree Expand file tree Collapse file tree 3 files changed +54
-0
lines changed Expand file tree Collapse file tree 3 files changed +54
-0
lines changed Original file line number Diff line number Diff line change @@ -150,6 +150,12 @@ def _parse_values(self, values):
150150 Parses values dict at init and sets object attributes with the
151151 values within.
152152 """
153+
154+ def list_parser (list_item ):
155+ if isinstance (list_item , dict ):
156+ return self .default_ret (list_item , api_kwargs = self .api_kwargs )
157+ return list_item
158+
153159 for k , v in values .items ():
154160
155161 if k not in JSON_FIELDS :
@@ -159,6 +165,10 @@ def _parse_values(self, values):
159165 v = lookup (v , api_kwargs = self .api_kwargs )
160166 else :
161167 v = self .default_ret (v , api_kwargs = self .api_kwargs )
168+
169+ if isinstance (v , list ):
170+ v = [list_parser (i ) for i in v ]
171+
162172 self ._add_cache ((k , v ))
163173 else :
164174 self ._add_cache ((k , v .copy ()))
@@ -238,6 +248,11 @@ def serialize(self, nested=False):
238248 if isinstance (current_val , netaddr .ip .IPNetwork ):
239249 current_val = str (current_val )
240250
251+ if isinstance (current_val , list ):
252+ current_val = [
253+ v .id if isinstance (v , Record ) else v
254+ for v in current_val
255+ ]
241256 ret [i ] = current_val
242257 return ret
243258
Original file line number Diff line number Diff line change 1+ import unittest
2+
3+ from pynetbox .lib .response import Record
4+
5+
6+ class RecordTestCase (unittest .TestCase ):
7+
8+ def test_serialize_list_of_records (self ):
9+ test_values = {
10+ 'id' : 123 ,
11+ "tagged_vlans" : [
12+ {
13+ "id" : 1 ,
14+ "url" : "http://localhost:8000/api/ipam/vlans/1/" ,
15+ "vid" : 1 ,
16+ "name" : "test1" ,
17+ "display_name" : "test1"
18+ },
19+ {
20+ "id" : 2 ,
21+ "url" : "http://localhost:8000/api/ipam/vlans/2/" ,
22+ "vid" : 2 ,
23+ "name" : "test 2" ,
24+ "display_name" : "test2"
25+ }
26+ ],
27+ }
28+ test_obj = Record (test_values )
29+ test = test_obj .serialize ()
30+ self .assertEqual (test ['tagged_vlans' ], [1 , 2 ])
31+
32+ def test_serialize_list_of_ints (self ):
33+ test_values = {
34+ 'id' : 123 ,
35+ 'units' : [12 ],
36+ }
37+ test_obj = Record (test_values )
38+ test = test_obj .serialize ()
39+ self .assertEqual (test ['units' ], [12 ])
You can’t perform that action at this time.
0 commit comments