netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
16.33k stars 2.6k forks source link

Extend `register_model_view()` to handle list views #18023

Closed jeremystretch closed 1 day ago

jeremystretch commented 1 week ago

Proposed Changes

We currently use register_model_view() to register detail views (edit, delete, etc.) for models, which obviates the need to declare explicit URL paths for each. This could be extended to support list views as well. For example, a common set of views might look like this:

# List view
@register_model_view(MyModel, list=True)
class FooListView(generic.ObjectListView):

# Detail view
@register_model_view(MyModel)
class MyModelView(generic.ObjectView):

# Add/edit views
@register_model_view(MyModel, 'edit')
@register_model_view(MyModel, 'add', list=True)
class MyModelEditView(generic.ObjectEditView):

# Delete view
@register_model_view(MyModel, 'delete')
class MyModelDeleteView(generic.ObjectDeleteView):

# Bulk import view
@register_model_view(MyModel, 'import', list=True)
class MyModelBulkImportView(generic.BulkImportView):

# Bulk delete view
@register_model_view(MyModel, 'delete', list=True)
class MyModelBulkDeleteView(generic.BulkDeleteView):

The corresponding set of URL paths could then be reduced to:

path('my-models/', include(get_model_urls('myapp', 'mymodel', list=True))),
path('my-models/<int:pk>/', include(get_model_urls('myapp', 'mymodel'))),

(This could potentially be reduced even further, but I'm also wary of over-optimizing.)

get_model_urls() will likewise need to be extended to differentiate between list and detail views.

Justification

This will allow us to reduce a substantial amount of boilerplate URL patterns without any breaking changes (AFAICT).

jeremystretch commented 2 days ago

Reclassifying this as a light feature request, as we expose register_model_view() in the plugins API.