API serializer foreign count fields do not have a default value #4353

Closed
opened 2025-12-29 18:35:06 +01:00 by adam · 4 comments
Owner

Originally created by @markkuleinio on GitHub (Dec 13, 2020).

Originally assigned to: @DanSheps on GitHub.

Environment

  • Python version: 3.7.3
  • NetBox version: 2.9.11

Steps to Reproduce

  1. Create a new tag with any name, like "Testing"
  2. Get the same tag via API and see tagged_items field

Expected Behavior

tagged_items is 0 (API docs say the field is integer)

Observed Behavior

tagged_items is null

$ curl -s -X GET "http://netbox-test.example.net/api/extras/tags/?name=Testing" -H "Authorization: token xxx" | jq .
{
  "count": 1,
  "next": null,
  "previous": null,
  "results": [
    {
      "id": 4,
      "url": "http://netbox-test.example.net/api/extras/tags/4/",
      "name": "Testing",
      "slug": "testing",
      "color": "9e9e9e",
      "description": "",
      "tagged_items": null
    }
  ]
}
Originally created by @markkuleinio on GitHub (Dec 13, 2020). Originally assigned to: @DanSheps on GitHub. ### Environment * Python version: 3.7.3 * NetBox version: 2.9.11 ### Steps to Reproduce 1. Create a new tag with any name, like "Testing" 2. Get the same tag via API and see `tagged_items` field <!-- What did you expect to happen? --> ### Expected Behavior `tagged_items` is 0 (API docs say the field is `integer`) <!-- What happened instead? --> ### Observed Behavior `tagged_items` is `null` ``` $ curl -s -X GET "http://netbox-test.example.net/api/extras/tags/?name=Testing" -H "Authorization: token xxx" | jq . { "count": 1, "next": null, "previous": null, "results": [ { "id": 4, "url": "http://netbox-test.example.net/api/extras/tags/4/", "name": "Testing", "slug": "testing", "color": "9e9e9e", "description": "", "tagged_items": null } ] } ```
adam added the type: bugstatus: accepted labels 2025-12-29 18:35:06 +01:00
adam closed this issue 2025-12-29 18:35:07 +01:00
Author
Owner

@jeremystretch commented on GitHub (Dec 14, 2020):

This happens in other areas, as well, when there are no related objects. Need to force null to zero somehow.

@jeremystretch commented on GitHub (Dec 14, 2020): This happens in other areas, as well, when there are no related objects. Need to force `null` to zero somehow.
Author
Owner

@DanSheps commented on GitHub (Dec 14, 2020):

Looks like we can do a default=0 in the serializer. This could be breaking though, perhaps we want to wait for 2.11?

@DanSheps commented on GitHub (Dec 14, 2020): Looks like we can do a default=0 in the serializer. This could be breaking though, perhaps we want to wait for 2.11?
Author
Owner

@jeremystretch commented on GitHub (Dec 14, 2020):

I'd argue that it's really fixing a bug rather than changing previously-desired behavior, so I think it's okay to change now.

@jeremystretch commented on GitHub (Dec 14, 2020): I'd argue that it's really fixing a bug rather than changing previously-desired behavior, so I think it's okay to change now.
Author
Owner

@DanSheps commented on GitHub (Dec 15, 2020):

I am going to change all instances of *_count = serializer.IntegerField and count_* = serializer.IntegerField to have default=0 in addition to this tagged_items field.

@DanSheps commented on GitHub (Dec 15, 2020): I am going to change all instances of `*_count = serializer.IntegerField` and `count_* = serializer.IntegerField` to have default=0 in addition to this tagged_items field.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4353