Character choice fields should be nullable in Models #7671

Closed
opened 2025-12-29 20:26:44 +01:00 by adam · 1 comment
Owner

Originally created by @arthanson on GitHub (Feb 22, 2023).

NetBox version

v3.4

Python version

3.8

Steps to Reproduce

Per Jeremy "I think in general, any model CharFields which store an optional selected value should be nullable". Django CharFields couldn't be nullable until somewhat recently

https://github.com/netbox-community/netbox/blob/develop/netbox/netbox/api/fields.py#L48

The ChoiceField returns None if blank, however for things like Cable:
https://github.com/netbox-community/netbox/blob/develop/netbox/dcim/models/cables.py#L71

The serializer goes off of the model definition which says it can't be None but the REST returns Null.

Expected Behavior

The model should be nullable so it is reflected correctly in the API

Observed Behavior

The model field shows it only allows blanks, but the API returns Nulls.

Originally created by @arthanson on GitHub (Feb 22, 2023). ### NetBox version v3.4 ### Python version 3.8 ### Steps to Reproduce Per Jeremy "I think in general, any model CharFields which store an optional selected value should be nullable". Django CharFields couldn't be nullable until somewhat recently https://github.com/netbox-community/netbox/blob/develop/netbox/netbox/api/fields.py#L48 The ChoiceField returns None if blank, however for things like Cable: https://github.com/netbox-community/netbox/blob/develop/netbox/dcim/models/cables.py#L71 The serializer goes off of the model definition which says it can't be None but the REST returns Null. ### Expected Behavior The model should be nullable so it is reflected correctly in the API ### Observed Behavior The model field shows it only allows blanks, but the API returns Nulls.
adam added the status: under reviewtype: housekeeping labels 2025-12-29 20:26:44 +01:00
adam closed this issue 2025-12-29 20:26:44 +01:00
Author
Owner

@jeremystretch commented on GitHub (Mar 16, 2023):

Per Jeremy "I think in general, any model CharFields which store an optional selected value should be nullable"

This was an incorrect assumption on my part. Per the Django documentation:

Avoid using null on string-based fields such as CharField and TextField. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL.

We should leave the models as they are and adjust the API schema accordingly where necessary.

@jeremystretch commented on GitHub (Mar 16, 2023): > Per Jeremy "I think in general, any model CharFields which store an optional selected value should be nullable" This was an incorrect assumption on my part. Per the [Django documentation](https://docs.djangoproject.com/en/4.1/ref/models/fields/#null): > Avoid using `null` on string-based fields such as CharField and TextField. If a string-based field has `null=True`, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL. We should leave the models as they are and adjust the API schema accordingly where necessary.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7671