Allow default "custom_field" setting for "selection" fields #1780

Closed
opened 2025-12-29 17:19:02 +01:00 by adam · 2 comments
Owner

Originally created by @bdlamprecht on GitHub (Jun 12, 2018).

Issue type

[X] Feature request

Environment

  • Python version: 3.5
  • NetBox version: 2.3.3

Description

This was brought up in the NetworkToCode Slack channel, I'm simply documenting the request here for recording purposes.

Currently, if you create a custom_field as a type Selection, any text entered into the Default is ignored (but is correctly remembered). Granted, the comments below that input area do state "N/A for selection fields". However, while briefly looking into the Django documentation for ModelChoiceField (which I believe is what you are using), there didn't seem to be any reason why this is not a valid option.

In the Slack channel, when the question was asked, the answer was "it was an issue with validation". I completely understand why you would want to validate that field, however, the same type of validation is not done for booleans either.

For example. if something besides "true" or "false" are entered as the default, such as "foobar", it is accepted but simply ignored when you try and create a new object for the model it the custom_field was created for.

I'm hoping for the same thing for a Selection field. If someone wants to set a valid option as the default and it is correct, use it, if it doesn't match a defined valid option, simply ignore it.

Any thoughts one way or the other?

Originally created by @bdlamprecht on GitHub (Jun 12, 2018). ### Issue type [X] Feature request ### Environment * Python version: 3.5 * NetBox version: 2.3.3 ### Description This was brought up in the NetworkToCode Slack channel, I'm simply documenting the request here for recording purposes. Currently, if you create a `custom_field` as a type `Selection`, any text entered into the `Default` is ignored (but is correctly remembered). Granted, the comments below that input area do state "N/A for selection fields". However, while briefly looking into the Django documentation for [ModelChoiceField](https://docs.djangoproject.com/en/2.0/ref/forms/fields/#modelchoicefield) (which I _believe_ is what you are using), there didn't seem to be any reason why this is not a valid option. In the Slack channel, when the question was asked, the answer was "it was an issue with validation". I completely understand why you would want to validate that field, however, the same type of validation is not done for booleans either. For example. if something _**besides**_ "true" or "false" are entered as the default, such as "foobar", it is accepted but simply ignored when you try and create a new object for the model it the `custom_field` was created for. I'm hoping for the same thing for a `Selection` field. If someone wants to set a valid option as the default and it is correct, use it, if it doesn't match a defined valid option, simply ignore it. Any thoughts one way or the other?
adam added the status: acceptedtype: feature labels 2025-12-29 17:19:02 +01:00
adam closed this issue 2025-12-29 17:19:03 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jun 29, 2018):

However, while briefly looking into the Django documentation for ModelChoiceField (which I believe is what you are using), there didn't seem to be any reason why this is not a valid option.

ModelChoiceField isn't used for custom fields. Custom field choices are stored in CustomFieldChoice instances and pulled in via the get_custom_fields_for_model() function:

# Select
elif cf.type == CF_TYPE_SELECT:
    choices = [(cfc.pk, cfc) for cfc in cf.choices.all()]
    if not cf.required or bulk_edit or filterable_only:
        choices = [(None, '---------')] + choices
    field = forms.TypedChoiceField(choices=choices, coerce=int, required=cf.required)

I suppose we can try to match the default value for a field to a choice with that label, and just silently fail if it doesn't exist.

@jeremystretch commented on GitHub (Jun 29, 2018): > However, while briefly looking into the Django documentation for ModelChoiceField (which I believe is what you are using), there didn't seem to be any reason why this is not a valid option. ModelChoiceField isn't used for custom fields. Custom field choices are stored in CustomFieldChoice instances and pulled in via the `get_custom_fields_for_model()` function: ``` # Select elif cf.type == CF_TYPE_SELECT: choices = [(cfc.pk, cfc) for cfc in cf.choices.all()] if not cf.required or bulk_edit or filterable_only: choices = [(None, '---------')] + choices field = forms.TypedChoiceField(choices=choices, coerce=int, required=cf.required) ``` I suppose we can try to match the default value for a field to a choice with that label, and just silently fail if it doesn't exist.
Author
Owner

@bdlamprecht commented on GitHub (Jun 29, 2018):

That works for me.

@bdlamprecht commented on GitHub (Jun 29, 2018): That works for me.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#1780