Skip to content

Commit d3fa77d

Browse files
committed
Use ChoiceFieldColumns on tables
1 parent b2fd39a commit d3fa77d

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

images/step05-accesslist-list.png

-244 Bytes
Loading

images/step06-accesslist2.png

167 Bytes
Loading

tutorial/step02-models.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,39 @@ class ProtocolChoices(ChoiceSet):
239239
Then, add the `choices` keyword argument to the `protocol` field:
240240

241241
```python
242-
# AccessListRule
242+
# AccessListRule
243243
protocol = models.CharField(
244244
max_length=30,
245245
choices=ProtocolChoices,
246246
blank=True
247247
)
248248
```
249249

250+
### Add Choice Color Methods
251+
252+
Now that we've defined choices for some of our model fields, we'll need to provide a method for returning the appropriate color for a selected choice. This works similar to Django's `get_FOO_display()` methods, but returns a color (defined on the field's `ChoiceSet`) rather than a label. This method will be called e.g. when displaying the field in a table.
253+
254+
Let's add a `get_default_action_color()` method on `AccessList`:
255+
256+
```python
257+
class AccessList(NetBoxModel):
258+
# ...
259+
def get_default_action_color(self):
260+
return ActionChoices.colors.get(self.default_action)
261+
```
262+
263+
We also need to add methods for `protocol` and `action` on `AccessListRule`:
264+
265+
```python
266+
class AccessListRule(NetBoxModel):
267+
# ...
268+
def get_protocol_color(self):
269+
return ProtocolChoices.colors.get(self.protocol)
270+
271+
def get_action_color(self):
272+
return ActionChoices.colors.get(self.action)
273+
```
274+
250275
## Create Schema Migrations
251276

252277
Now that we have our models defined, we need to generate a schema for the PostgreSQL database. While it's possible to create the tables and constraints by hand, it's _much_ easier to employ Django's [migrations feature](https://docs.djangoproject.com/en/4.0/topics/migrations/). This will inspect our model classes and generate the necessary migration files automatically. This is a two-step process: First we generate the migration file with the `makemigrations` management command, then we run `migrate` to apply it to the live database.

tutorial/step03-tables.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ $ cd netbox_access_lists/
1515
$ edit tables.py
1616
```
1717

18-
At the top of this file, import the `django-tables2` library, as well as NetBox's `NetBoxTable` class. The latter will serve as the base class for our tables. We'll also import our plugin's models from `models.py`.
18+
At the top of this file, import the `django-tables2` library. This will provide the column classes for fields we wish to customize. We'll also import NetBox's `NetBoxTable` class, which will serve as the base class for our tables, and `ChoiceFieldColumn`. Finally we import our plugin's models from `models.py`.
1919

2020
```python
2121
import django_tables2 as tables
2222

23-
from netbox.tables import NetBoxTable
23+
from netbox.tables import NetBoxTable, ChoiceFieldColumn
2424
from .models import AccessList, AccessListRule
2525
```
2626

@@ -50,13 +50,20 @@ class AccessListTable(NetBoxTable):
5050
)
5151
```
5252

53+
Also, recall that the `default_action` field on the `AccessList` model is a choice field, with a color assigned to each choice. To display these values, we'll use NetBox's `ChoiceFieldColumn` class.
54+
55+
```python
56+
default_action = ChoiceFieldColumn()
57+
```
58+
5359
It would also be nice to include a count showing the number of rules each access list has assigned to it. We can add a custom column named `rule_count` to show this. (The data for this column will be annotated by the view; more on this in step five.) We'll also need to add this column to our `fields` and (optionally) `default_columns` under the `Meta` subclass. Our finished table should look like this:
5460

5561
```python
5662
class AccessListTable(NetBoxTable):
5763
name = tables.Column(
5864
linkify=True
5965
)
66+
default_action = ChoiceFieldColumn()
6067
rule_count = tables.Column()
6168

6269
class Meta(NetBoxTable.Meta):
@@ -67,7 +74,7 @@ class AccessListTable(NetBoxTable):
6774

6875
### AccessListRuleTable
6976

70-
We'll also create a table for our `AccessListRule` model using the same approach as above. In this case, we don't need to add any custom columns, but we do want to linkify the `access_list` and `index` columns. The former will link to the parent access list, and the latter will link to the individual rule.
77+
We'll also create a table for our `AccessListRule` model using the same approach as above. Start by linkifying the `access_list` and `index` columns. The former will link to the parent access list, and the latter will link to the individual rule. We also want to declare `protocol` and `action` as `ChoiceFieldColumn` instances.
7178

7279
```python
7380
class AccessListRuleTable(NetBoxTable):
@@ -77,6 +84,8 @@ class AccessListRuleTable(NetBoxTable):
7784
index = tables.Column(
7885
linkify=True
7986
)
87+
protocol = ChoiceFieldColumn()
88+
action = ChoiceFieldColumn()
8089

8190
class Meta(NetBoxTable.Meta):
8291
model = AccessListRule

0 commit comments

Comments
 (0)