Skip to content

Commit a0a093b

Browse files
author
Zach Moody
committed
Fixes Nested Record Write methods
Previously if save() or delete() were called on nested records the URL called would be that of the parent. This fixes #161 and #212 by correcting that behavior.
1 parent 479f0db commit a0a093b

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

pynetbox/core/query.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def __init__(
140140
private_key=None,
141141
session_key=None,
142142
ssl_verify=True,
143+
url=None,
143144
):
144145
"""
145146
Instantiates a new Request object

pynetbox/core/response.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ def save(self):
384384
if diff:
385385
serialized = self.serialize()
386386
req = Request(
387-
key=self.id,
388-
base=self.endpoint.url,
387+
key=self.id if not self.url else None,
388+
base=self.url or self.endpoint.url,
389389
token=self.api.token,
390390
session_key=self.api.session_key,
391391
ssl_verify=self.api.ssl_verify,
@@ -433,8 +433,8 @@ def delete(self):
433433
>>>
434434
"""
435435
req = Request(
436-
key=self.id,
437-
base=self.endpoint.url,
436+
key=self.id if not self.url else None,
437+
base=self.url or self.endpoint.url,
438438
token=self.api.token,
439439
session_key=self.api.session_key,
440440
ssl_verify=self.api.ssl_verify,

tests/unit/test_response.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,21 @@ def test_compare(self):
139139
test2 = Record({}, None, endpoint2)
140140
test2.id = 1
141141
self.assertEqual(test1, test2)
142+
143+
def test_nested_write(self):
144+
app = Mock()
145+
app.token = 'abc123'
146+
endpoint = Mock()
147+
endpoint.name = "test-endpoint"
148+
test = Record({
149+
'id': 123,
150+
'name': 'test',
151+
'child': {
152+
'id': 321,
153+
'name': 'test123',
154+
'url': 'http://localhost:8080/test',
155+
},
156+
}, app, endpoint)
157+
test.child.name = 'test321'
158+
test.child.save()
159+
self.assertEqual(app.http_session.patch.call_args[0][0], "http://localhost:8080/test/")

0 commit comments

Comments
 (0)