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

Introduce choice sets for custom fields #12988

Closed
jeremystretch opened this issue Jun 23, 2023 · 0 comments
Closed

Introduce choice sets for custom fields #12988

jeremystretch opened this issue Jun 23, 2023 · 0 comments
Assignees
Labels
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

v3.5.4

Feature type

Data model extension

Proposed functionality

Currently, custom field choices are stored as an array of values on each applicable CustomField instance:

class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel):
    choices = ArrayField(
        base_field=models.CharField(max_length=100),
        blank=True,
        null=True,
        help_text=_('Comma-separated list of available choices (for selection fields)')
    )

This issue proposes essentially moving the choices field to a separate model, CustomFieldChoiceSet:

class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel):
    choices = models.ForeignKey(
        to='CustomFieldChoiceSet',
        blank=True,
        null=True
    )


class CustomFieldChoiceSet(ChangeLoggedModel):
    name = models.CharField()
    description = models.CharField(blank=True)
    choices = ArrayField(
        base_field=models.CharField(max_length=100),
        help_text=_('Comma-separated list of available choices (for selection fields)')
    )

Individual choice values will continue to be managed as flat lists, but now are defined on separate CustomFieldChoiceSet instances, which can be referenced by multiple custom fields.

As part of this work, I'd also like to improve the manner in which custom field choices are managed by the end user. We can extend the current view to allow creating, modifying, and deleting choice values as if they were individual objects (as opposed to a list of values in a single JSON form field).

This will result in a breaking API change for custom fields themselves, however custom field data on NetBox objects should not be affected by the change.

Use case

This change was prompted by #12194, which seeks to introduce reusable choice sets containing well-known values (e.g. UN-LOCODE identifiers). Moving the choice sets into a separate model allows for their efficient reuse across multiple custom fields without introducing significant overhead. (The choice sets can be easily pre-fetched along with the custom fields themselves.)

Database changes

  • Introduce the new CustomFieldChoiceSet model (above)
  • Add a ForeignKey from CustomField to CustomFieldChoiceSet
  • Migrate all existing choices values to new CustomFieldChoiceSet instances
  • Drop the old choices field from the CustomField model

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 Jun 23, 2023
@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation and removed status: under review Further discussion is needed to determine this issue's scope and/or implementation labels Jun 26, 2023
@jeremystretch jeremystretch added this to the v3.6 milestone Jun 26, 2023
@jeremystretch jeremystretch self-assigned this Jun 26, 2023
jeremystretch added a commit that referenced this issue Jul 19, 2023
* Initial work on custom field choice sets

* Rename choices to extra_choices (prep for #12194)

* Remove CustomField.choices

* Add & update tests

* Clean up table columns

* Add order_alphanetically boolean for choice sets

* Introduce ArrayColumn for choice lists

* Show dependent custom fields on choice set view

* Update custom fields documentation

* Introduce ArrayWidget for more convenient editing of choices

* Incorporate PR feedback

* Misc cleanup
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
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