Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert all optional choice fields to use a null empty value #17761

Closed
jeremystretch opened this issue Oct 15, 2024 · 1 comment
Closed

Convert all optional choice fields to use a null empty value #17761

jeremystretch opened this issue Oct 15, 2024 · 1 comment
Assignees
Labels
complexity: medium Requires a substantial but not unusual amount of effort to implement netbox status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application
Milestone

Comments

@jeremystretch
Copy link
Member

NetBox version

v4.1.3

Feature type

Change to existing functionality

Triage priority

N/A

Proposed functionality

Several models include optional choices fields, such as Cable.type. Most (if not all) of these store an empty value as an empty string ('') rather than null. This FR proposes:

  1. Changing the empty value of these fields to null
  2. Introducing migrations to replace all empty string values with null for these fields

The list of specific fields to be updated is TBD, but is expect to include any CharField for which choices is defined and null is False.

Use case

The use of empty strings to represent empty values for these fields deviates from other fields (e.g. integer-based fields) and complicates the logic needed to achieve complex filtering (see #17575 for an example). This change will help us standardize the behavior of the data model in general.

Database changes

All CharFields which define a set of choices will be modified to store empty values as null in the database. Migrations will be included to effect this change on existing data.

External dependencies

N/A

@jeremystretch jeremystretch added type: feature Introduction of new functionality to the application status: under review Further discussion is needed to determine this issue's scope and/or implementation labels Oct 15, 2024
@jeremystretch
Copy link
Member Author

jeremystretch commented Oct 16, 2024

These are the affected fields:

  • circuits.CircuitGroupAssignment: priority
  • circuits.CircuitTermination: cable_end
  • dcim.ConsolePort: cable_end, type
  • dcim.ConsoleServerPort: cable_end, type
  • dcim.PowerPort: cable_end, type
  • dcim.PowerOutlet: cable_end, type, feed_leg
  • dcim.Interface: cable_end, mode, rf_role, rf_channel, poe_mode, poe_type
  • dcim.FrontPort: cable_end
  • dcim.RearPort: cable_end
  • dcim.Cable: type, length_unit
  • dcim.ConsolePortTemplate: type
  • dcim.ConsoleServerPortTemplate: type
  • dcim.PowerPortTemplate: type
  • dcim.PowerOutletTemplate: type, feed_leg
  • dcim.InterfaceTemplate: poe_mode, poe_type, rf_role
  • dcim.DeviceType: weight_unit, subdevice_role, airflow
  • dcim.ModuleType: weight_unit, airflow
  • dcim.Device: face, airflow
  • dcim.PowerFeed: cable_end
  • dcim.RackType: weight_unit, outer_unit
  • dcim.Rack: weight_unit, outer_unit, form_factor, airflow
  • ipam.FHRPGroup: auth_type
  • ipam.IPAddress: role
  • extras.CustomFieldChoiceSet: base_choices
  • tenancy.ContactAssignment: priority
  • virtualization.VMInterface: mode
  • vpn.IKEProposal: authentication_algorithm
  • vpn.IKEPolicy: mode
  • vpn.IPSecProposal: encryption_algorithm, authentication_algorithm
  • wireless.WirelessLAN: auth_type, auth_cipher
  • wireless.WirelessLink: auth_type, auth_cipher, distance_unit
Audit script
from django.apps import apps
from django.db import models

for model in apps.get_models():
    fields = []
    for field in model._meta.get_fields():
        if isinstance(field, models.CharField) and field.choices and field.blank and not field.null:
            fields.append(f"`{field.name}`")
    if fields:
        print(f"* {model._meta.label}: {', '.join(fields)}")

@jeremystretch jeremystretch self-assigned this Oct 16, 2024
@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation complexity: medium Requires a substantial but not unusual amount of effort to implement and removed status: under review Further discussion is needed to determine this issue's scope and/or implementation labels Oct 16, 2024
@jeremystretch jeremystretch added this to the v4.2 milestone Oct 16, 2024
@jeremystretch jeremystretch added v4.2 and removed v4.2 labels Oct 18, 2024
@jeremystretch jeremystretch added the netbox label Nov 1, 2024 — with Linear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complexity: medium Requires a substantial but not unusual amount of effort to implement netbox status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application
Projects
None yet
Development

No branches or pull requests

1 participant