Skip to content

Devise a mechanism for referencing all user-facing models #13427

@jeremystretch

Description

@jeremystretch

Proposed Changes

Devise and implement a mechanism for filtering the content types for all user-facing models in NetBox. This includes essentially all of the models which can be manipulated directly via the UI or API, and excludes those "behind the scenes" models which cannot.

Justification

There are several instances where we currently work around this limitation. Below are some that I have identified; there are likely others to address as well.

  1. Retrieving content types for e.g. the object list dashboard widget:
# extras/dashboard/widgets.py
def get_content_type_labels():
    return [
        (content_type_identifier(ct), content_type_name(ct))
        for ct in ContentType.objects.filter(
            FeatureQuery('export_templates').get_query() | Q(app_label='extras', model='objectchange') |
            Q(app_label='extras', model='configcontext')
        ).order_by('app_label', 'model')
    ]
  1. Limiting object_type choices on CustomField import:
# extras/forms/bulk_import.py
class CustomFieldImportForm(CSVModelForm):
    ...
    object_type = CSVContentTypeField(
        label=_('Object type'),
        queryset=ContentType.objects.all(),
        limit_choices_to=FeatureQuery('custom_fields'),  # Should be generic
        required=False,
        help_text=_("Object type (for object or multi-object fields)")
    )
  1. Filtering for SavedFilter objects:
# extras/forms/filtersets.py
class SavedFilterFilterForm(SavedFiltersMixin, FilterForm):
    content_types = ContentTypeMultipleChoiceField(
        label=_('Content types'),
        queryset=ContentType.objects.filter(FeatureQuery('export_templates').get_query()),  # Should be generic
        required=False
    )
  1. Limiting object_type choices on CustomField edit:
# extras/forms/model_forms.py
class CustomFieldForm(BootstrapMixin, forms.ModelForm):
    object_type = ContentTypeChoiceField(
        label=_('Object type'),
        queryset=ContentType.objects.all(),
        # TODO: Come up with a canonical way to register suitable models
        limit_choices_to=FeatureQuery('webhooks').get_query() | Q(app_label='auth', model__in=['user', 'group']),
        required=False,
        help_text=_("Type of the related object (for object/multi-object fields only)")
    )

Metadata

Metadata

Assignees

Labels

status: acceptedThis issue has been accepted for implementationtype: housekeepingChanges to the application which do not directly impact the end user

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions