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

Create custom form field for choice-based filters #1866

Closed
jeremystretch opened this issue Feb 6, 2018 · 0 comments
Closed

Create custom form field for choice-based filters #1866

jeremystretch opened this issue Feb 6, 2018 · 0 comments
Labels
status: accepted This issue has been accepted for implementation type: housekeeping Changes to the application which do not directly impact the end user

Comments

@jeremystretch
Copy link
Member

Issue type

[x] Feature request
[ ] Bug report
[ ] Documentation

Environment

  • Python version: 3.5.2
  • NetBox version: 2.2.9

Description

NetBox currently employs custom functions to generate the options for choice-based fields in filter forms. For example, the following is for site status selection:

def site_status_choices():
    status_counts = {}
    for status in Site.objects.values('status').annotate(count=Count('status')).order_by('status'):
        status_counts[status['status']] = status['count']
    return [(s[0], '{} ({})'.format(s[1], status_counts.get(s[0], 0))) for s in SITE_STATUS_CHOICES]

class SiteFilterForm(BootstrapMixin, CustomFieldFilterForm):
    model = Site
    status = forms.MultipleChoiceField(choices=site_status_choices, required=False)
    ...

The logic used to calculate and attach the count of each option is replicated in the same manner for several fields. We should be able to devise a simple custom form field that can be re-used without duplicating this boilerplate code.

@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation type: housekeeping Changes to the application which do not directly impact the end user labels Feb 6, 2018
@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: housekeeping Changes to the application which do not directly impact the end user
Projects
None yet
Development

No branches or pull requests

1 participant