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

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

Closed
bdlamprecht opened this issue Jun 11, 2018 · 2 comments
Closed

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

bdlamprecht opened this issue Jun 11, 2018 · 2 comments
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application

Comments

@bdlamprecht
Copy link
Contributor

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?

@jeremystretch
Copy link
Member

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 jeremystretch added status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application labels Jun 29, 2018
@bdlamprecht
Copy link
Contributor Author

That works for me.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 17, 2020
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

2 participants