mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 01:41:22 -06:00
Add form rendering utilities to plugins dev docs
This commit is contained in:
parent
708d93c9e0
commit
89150f4b27
@ -62,10 +62,11 @@ class Circuit(PrimaryModel):
|
|||||||
|
|
||||||
1. Import `gettext_lazy` as `_`.
|
1. Import `gettext_lazy` as `_`.
|
||||||
2. All form fields must specify a `label` wrapped with `gettext_lazy()`.
|
2. All form fields must specify a `label` wrapped with `gettext_lazy()`.
|
||||||
3. All headers under a form's `fieldsets` property must be wrapped with `gettext_lazy()`.
|
3. The name of each FieldSet on a form must be wrapped with `gettext_lazy()`.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
from utilities.forms.rendering import FieldSet
|
||||||
|
|
||||||
class CircuitBulkEditForm(NetBoxModelBulkEditForm):
|
class CircuitBulkEditForm(NetBoxModelBulkEditForm):
|
||||||
description = forms.CharField(
|
description = forms.CharField(
|
||||||
@ -74,7 +75,7 @@ class CircuitBulkEditForm(NetBoxModelBulkEditForm):
|
|||||||
)
|
)
|
||||||
|
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
(_('Circuit'), ('provider', 'type', 'status', 'description')),
|
FieldSet('provider', 'type', 'status', 'description', name=_('Circuit')),
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -15,16 +15,18 @@ NetBox provides several base form classes for use by plugins.
|
|||||||
|
|
||||||
This is the base form for creating and editing NetBox models. It extends Django's ModelForm to add support for tags and custom fields.
|
This is the base form for creating and editing NetBox models. It extends Django's ModelForm to add support for tags and custom fields.
|
||||||
|
|
||||||
| Attribute | Description |
|
| Attribute | Description |
|
||||||
|-------------|-------------------------------------------------------------|
|
|-------------|---------------------------------------------------------------------------------------|
|
||||||
| `fieldsets` | A tuple of two-tuples defining the form's layout (optional) |
|
| `fieldsets` | A tuple of `FieldSet` instances which control how form fields are rendered (optional) |
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
from dcim.models import Site
|
from dcim.models import Site
|
||||||
from netbox.forms import NetBoxModelForm
|
from netbox.forms import NetBoxModelForm
|
||||||
from utilities.forms.fields import CommentField, DynamicModelChoiceField
|
from utilities.forms.fields import CommentField, DynamicModelChoiceField
|
||||||
|
from utilities.forms.rendering import FieldSet
|
||||||
from .models import MyModel
|
from .models import MyModel
|
||||||
|
|
||||||
class MyModelForm(NetBoxModelForm):
|
class MyModelForm(NetBoxModelForm):
|
||||||
@ -33,8 +35,8 @@ class MyModelForm(NetBoxModelForm):
|
|||||||
)
|
)
|
||||||
comments = CommentField()
|
comments = CommentField()
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
('Model Stuff', ('name', 'status', 'site', 'tags')),
|
FieldSet('name', 'status', 'site', 'tags', name=_('Model Stuff')),
|
||||||
('Tenancy', ('tenant_group', 'tenant')),
|
FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
|
||||||
)
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
@ -52,6 +54,7 @@ This form facilitates the bulk import of new objects from CSV, JSON, or YAML dat
|
|||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
from dcim.models import Site
|
from dcim.models import Site
|
||||||
from netbox.forms import NetBoxModelImportForm
|
from netbox.forms import NetBoxModelImportForm
|
||||||
from utilities.forms import CSVModelChoiceField
|
from utilities.forms import CSVModelChoiceField
|
||||||
@ -62,7 +65,7 @@ class MyModelImportForm(NetBoxModelImportForm):
|
|||||||
site = CSVModelChoiceField(
|
site = CSVModelChoiceField(
|
||||||
queryset=Site.objects.all(),
|
queryset=Site.objects.all(),
|
||||||
to_field_name='name',
|
to_field_name='name',
|
||||||
help_text='Assigned site'
|
help_text=_('Assigned site')
|
||||||
)
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
@ -77,16 +80,18 @@ This form facilitates editing multiple objects in bulk. Unlike a model form, thi
|
|||||||
| Attribute | Description |
|
| Attribute | Description |
|
||||||
|-------------------|---------------------------------------------------------------------------------------------|
|
|-------------------|---------------------------------------------------------------------------------------------|
|
||||||
| `model` | The model of object being edited |
|
| `model` | The model of object being edited |
|
||||||
| `fieldsets` | A tuple of two-tuples defining the form's layout (optional) |
|
| `fieldsets` | A tuple of `FieldSet` instances which control how form fields are rendered (optional) |
|
||||||
| `nullable_fields` | A tuple of fields which can be nullified (set to empty) using the bulk edit form (optional) |
|
| `nullable_fields` | A tuple of fields which can be nullified (set to empty) using the bulk edit form (optional) |
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from django import forms
|
from django import forms
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
from dcim.models import Site
|
from dcim.models import Site
|
||||||
from netbox.forms import NetBoxModelImportForm
|
from netbox.forms import NetBoxModelImportForm
|
||||||
from utilities.forms import CommentField, DynamicModelChoiceField
|
from utilities.forms import CommentField, DynamicModelChoiceField
|
||||||
|
from utilities.forms.rendering import FieldSet
|
||||||
from .models import MyModel, MyModelStatusChoices
|
from .models import MyModel, MyModelStatusChoices
|
||||||
|
|
||||||
|
|
||||||
@ -106,7 +111,7 @@ class MyModelEditForm(NetBoxModelImportForm):
|
|||||||
|
|
||||||
model = MyModel
|
model = MyModel
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
('Model Stuff', ('name', 'status', 'site')),
|
FieldSet('name', 'status', 'site', name=_('Model Stuff')),
|
||||||
)
|
)
|
||||||
nullable_fields = ('site', 'comments')
|
nullable_fields = ('site', 'comments')
|
||||||
```
|
```
|
||||||
@ -115,10 +120,10 @@ class MyModelEditForm(NetBoxModelImportForm):
|
|||||||
|
|
||||||
This form class is used to render a form expressly for filtering a list of objects. Its fields should correspond to filters defined on the model's filter set.
|
This form class is used to render a form expressly for filtering a list of objects. Its fields should correspond to filters defined on the model's filter set.
|
||||||
|
|
||||||
| Attribute | Description |
|
| Attribute | Description |
|
||||||
|-------------------|-------------------------------------------------------------|
|
|-------------|---------------------------------------------------------------------------------------|
|
||||||
| `model` | The model of object being edited |
|
| `model` | The model of object being edited |
|
||||||
| `fieldsets` | A tuple of two-tuples defining the form's layout (optional) |
|
| `fieldsets` | A tuple of `FieldSet` instances which control how form fields are rendered (optional) |
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
@ -206,3 +211,13 @@ In addition to the [form fields provided by Django](https://docs.djangoproject.c
|
|||||||
::: utilities.forms.fields.CSVMultipleContentTypeField
|
::: utilities.forms.fields.CSVMultipleContentTypeField
|
||||||
options:
|
options:
|
||||||
members: false
|
members: false
|
||||||
|
|
||||||
|
## Form Rendering
|
||||||
|
|
||||||
|
::: utilities.forms.rendering.FieldSet
|
||||||
|
|
||||||
|
::: utilities.forms.rendering.InlineFields
|
||||||
|
|
||||||
|
::: utilities.forms.rendering.TabbedGroups
|
||||||
|
|
||||||
|
::: utilities.forms.rendering.ObjectAttribute
|
||||||
|
Loading…
Reference in New Issue
Block a user