POST- created devices do not appear in their rack's location's device count. #7803

Closed
opened 2025-12-29 20:28:23 +01:00 by adam · 4 comments
Owner

Originally created by @dpotter-intoto on GitHub (Mar 28, 2023).

Originally assigned to: @arthanson on GitHub.

NetBox version

3.4.6

Python version

3.9

Steps to Reproduce

  1. Identify a rack that is assigned to a location and which has space to contain a new device
  2. Browse to the location and note the number of devices present in that location.
  3. Use the REST API to create a new device. (POST /api/dcim/devices/). Assign the device to the rack, but not to the location.
  4. Browse to the location again and note that the number of devices has not changed.
  5. Edit the rack, changing something minor like a description or serial number.
  6. Browse to the location again and note that the number of devices has now incremented to include the new device

Expected Behavior

I expected the location to include the device count for the new device (via the device-rack-location relationship)

Observed Behavior

The location does not include the new device in its device-count.

Originally created by @dpotter-intoto on GitHub (Mar 28, 2023). Originally assigned to: @arthanson on GitHub. ### NetBox version 3.4.6 ### Python version 3.9 ### Steps to Reproduce 1. Identify a rack that is assigned to a location and which has space to contain a new device 2. Browse to the location and note the number of devices present in that location. 3. Use the REST API to create a new device. (POST `/api/dcim/devices/`). Assign the device to the rack, but not to the location. 4. Browse to the location again and note that the number of devices has not changed. 5. Edit the rack, changing something minor like a description or serial number. 6. Browse to the location again and note that the number of devices has now incremented to include the new device ### Expected Behavior I expected the location to include the device count for the new device (via the device-rack-location relationship) ### Observed Behavior The location does not include the new device in its device-count.
adam added the type: bugstatus: accepted labels 2025-12-29 20:28:23 +01:00
adam closed this issue 2025-12-29 20:28:23 +01:00
Author
Owner

@dpotter-intoto commented on GitHub (Mar 28, 2023):

Comments:

Possibly this is working as intended?

If I include the location in the POST where I create the device, it is properly included.
If I edit and update the rack that contains the device, the location is also updated.

@dpotter-intoto commented on GitHub (Mar 28, 2023): Comments: Possibly this is working as intended? If I include the location in the POST where I create the device, it is properly included. If I edit and update the rack that contains the device, the location is also updated.
Author
Owner

@kkthxbye-code commented on GitHub (Mar 28, 2023):

No, it's a bug. When creating a device without the location set, but the rack has a location, the following code sets the location:

96eb89a469/netbox/dcim/models/devices.py (L658-L663)

I've verified in the debugger that this also happens when creating the device via. the API, however the location is not present when the object is finally saved. No idea where the bug actually lies though.

@kkthxbye-code commented on GitHub (Mar 28, 2023): No, it's a bug. When creating a device without the location set, but the rack has a location, the following code sets the location: https://github.com/netbox-community/netbox/blob/96eb89a469b255a209d80555d487b1cdddd6062f/netbox/dcim/models/devices.py#L658-L663 I've verified in the debugger that this also happens when creating the device via. the API, however the location is not present when the object is finally saved. No idea where the bug actually lies though.
Author
Owner

@kkthxbye-code commented on GitHub (Mar 28, 2023):

Seems like modifications to the object in the Device.clean() method are not persisted when saving through the API. I tried setting doing the following in the model clean method:

self.name = "testbloop123"

When creating a device through the UI, the device is called testbloop123 regardless of name given. When saving through the API, the device is called whatever is passed in the name field. I also tried with the location model, here the modifications done in clear are also ignored when using the REST API.

@jeremystretch do you know what is going on here? Seems pretty bad as we quite often modify the object in the clean method of models.

@kkthxbye-code commented on GitHub (Mar 28, 2023): Seems like modifications to the object in the Device.clean() method are not persisted when saving through the API. I tried setting doing the following in the model clean method: `self.name = "testbloop123"` When creating a device through the UI, the device is called testbloop123 regardless of name given. When saving through the API, the device is called whatever is passed in the name field. I also tried with the location model, here the modifications done in clear are also ignored when using the REST API. @jeremystretch do you know what is going on here? Seems pretty bad as we quite often modify the object in the clean method of models.
Author
Owner

@arthanson commented on GitHub (Mar 30, 2023):

I think the issue is here: https://github.com/netbox-community/netbox/blob/develop/netbox/netbox/api/serializers/base.py#L41
DRF doesn't call the model clean, NetBox does put it in here but it copies the data then calls clean, so clean only effects the copy of the data, the returned data (which eventually gets saved is never modified). Probably need a loop to go through the fields after the full_clean call...

I guess a bigger question is should the data be getting modified in the clean method?

@arthanson commented on GitHub (Mar 30, 2023): I think the issue is here: https://github.com/netbox-community/netbox/blob/develop/netbox/netbox/api/serializers/base.py#L41 DRF doesn't call the model clean, NetBox does put it in here but it copies the data then calls clean, so clean only effects the copy of the data, the returned data (which eventually gets saved is never modified). Probably need a loop to go through the fields after the full_clean call... I guess a bigger question is should the data be getting modified in the clean method?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7803