Skip to content

Commit a97b438

Browse files
authored
Fixes #19530: Overhaul documentation for plugin views (#19530)
1 parent b3d318c commit a97b438

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

docs/plugins/development/views.md

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Views
22

3-
## Writing Views
3+
## Writing Basic Views
44

55
If your plugin will provide its own page or pages within the NetBox web UI, you'll need to define views. A view is a piece of business logic which performs an action and/or renders a page when a request is made to a particular URL. HTML content is rendered using a [template](./templates.md). Views are typically defined in `views.py`, and URL patterns in `urls.py`.
66

@@ -47,9 +47,13 @@ A URL pattern has three components:
4747

4848
This makes our view accessible at the URL `/plugins/animal-sounds/random/`. (Remember, our `AnimalSoundsConfig` class sets our plugin's base URL to `animal-sounds`.) Viewing this URL should show the base NetBox template with our custom content inside it.
4949

50+
## NetBox Model Views
51+
52+
NetBox provides several generic view classes and additional helper functions, to simplify the implementation of plugin logic. These are recommended to be used whenever possible to keep the maintenance overhead of plugins low.
53+
5054
### View Classes
5155

52-
NetBox provides several generic view classes (documented below) to facilitate common operations, such as creating, viewing, modifying, and deleting objects. Plugins can subclass these views for their own use.
56+
Generic view classes (documented below) facilitate common operations, such as creating, viewing, modifying, and deleting objects. Plugins can subclass these views for their own use.
5357

5458
| View Class | Description |
5559
|----------------------|--------------------------------------------------------|
@@ -65,18 +69,51 @@ NetBox provides several generic view classes (documented below) to facilitate co
6569
!!! warning
6670
Please note that only the classes which appear in this documentation are currently supported. Although other classes may be present within the `views.generic` module, they are not yet supported for use by plugins.
6771

68-
#### Example Usage
72+
### URL registration
73+
74+
The NetBox URL registration process has two parts:
75+
76+
1. View classes can be decorated with `@register_model_view()`. This registers a new URL for the model.
77+
2. All of a model's URLs can be included in `urls.py` using the `get_model_urls()` function. This call is usually required twice: once to import general views for the model and again to import model detail views tied to the object's primary key.
78+
79+
::: utilities.views.register_model_view
80+
81+
!!! note "Changed in NetBox v4.2"
82+
In NetBox v4.2, the `register_model_view()` function was extended to support the registration of list views by passing `detail=False`.
83+
84+
::: utilities.urls.get_model_urls
85+
86+
!!! note "Changed in NetBox v4.2"
87+
In NetBox v4.2, the `get_model_urls()` function was extended to support retrieving registered general model views (e.g. for listing objects) by passing `detail=False`.
88+
89+
### Example Usage
6990

7091
```python
7192
# views.py
7293
from netbox.views.generic import ObjectEditView
94+
from utilities.views import register_model_view
7395
from .models import Thing
7496

97+
@register_model_view(Thing, name='add', detail=False)
98+
@register_model_view(Thing, name='edit')
7599
class ThingEditView(ObjectEditView):
76100
queryset = Thing.objects.all()
77101
template_name = 'myplugin/thing.html'
78102
...
79103
```
104+
105+
```python
106+
# urls.py
107+
from django.urls import include, path
108+
from utilities.urls import get_model_urls
109+
110+
urlpatterns = [
111+
path('thing/', include(get_model_urls('myplugin', 'thing', detail=False))),
112+
path('thing/<int:pk>/', include(get_model_urls('myplugin', 'thing'))),
113+
...
114+
]
115+
```
116+
80117
## Object Views
81118

82119
Below are the class definitions for NetBox's object views. These views handle CRUD actions for individual objects. The view, add/edit, and delete views each inherit from `BaseObjectView`, which is not intended to be used directly.
@@ -143,6 +180,9 @@ Below are the class definitions for NetBox's multi-object views. These views han
143180

144181
These views are provided to enable or enhance certain NetBox model features, such as change logging or journaling. These typically do not need to be subclassed: They can be used directly e.g. in a URL path.
145182

183+
!!! note
184+
These feature views are automatically registered for all models that implement the respective feature. There is usually no need to override them. However, if that's the case, the URL must be registered manually in `urls.py` instead of using the `register_model_view()` function or decorator.
185+
146186
::: netbox.views.generic.ObjectChangeLogView
147187
options:
148188
members:
@@ -157,7 +197,7 @@ These views are provided to enable or enhance certain NetBox model features, suc
157197

158198
### Additional Tabs
159199

160-
Plugins can "attach" a custom view to a core NetBox model by registering it with `register_model_view()`. To include a tab for this view within the NetBox UI, declare a TabView instance named `tab`, and add it to the template context dict:
200+
Plugins can "attach" a custom view to a NetBox model by registering it with `register_model_view()`. To include a tab for this view within the NetBox UI, declare a TabView instance named `tab`, and add it to the template context dict:
161201

162202
```python
163203
from dcim.models import Site
@@ -185,11 +225,6 @@ class MyView(generic.ObjectView):
185225
)
186226
```
187227

188-
!!! note "Changed in NetBox v4.2"
189-
The `register_model_view()` function was extended in NetBox v4.2 to support registration of list views by passing `detail=False`.
190-
191-
::: utilities.views.register_model_view
192-
193228
::: utilities.views.ViewTab
194229

195230
### Extra Template Content

0 commit comments

Comments
 (0)