You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorial/step02-models.md
+26-1
Original file line number
Diff line number
Diff line change
@@ -239,14 +239,39 @@ class ProtocolChoices(ChoiceSet):
239
239
Then, add the `choices` keyword argument to the `protocol` field:
240
240
241
241
```python
242
-
# AccessListRule
242
+
# AccessListRule
243
243
protocol = models.CharField(
244
244
max_length=30,
245
245
choices=ProtocolChoices,
246
246
blank=True
247
247
)
248
248
```
249
249
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`:
We also need to add methods for `protocol` and `action` on `AccessListRule`:
264
+
265
+
```python
266
+
classAccessListRule(NetBoxModel):
267
+
# ...
268
+
defget_protocol_color(self):
269
+
return ProtocolChoices.colors.get(self.protocol)
270
+
271
+
defget_action_color(self):
272
+
return ActionChoices.colors.get(self.action)
273
+
```
274
+
250
275
## Create Schema Migrations
251
276
252
277
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.
Copy file name to clipboardExpand all lines: tutorial/step03-tables.md
+12-3
Original file line number
Diff line number
Diff line change
@@ -15,12 +15,12 @@ $ cd netbox_access_lists/
15
15
$ edit tables.py
16
16
```
17
17
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`.
19
19
20
20
```python
21
21
import django_tables2 as tables
22
22
23
-
from netbox.tables import NetBoxTable
23
+
from netbox.tables import NetBoxTable, ChoiceFieldColumn
24
24
from .models import AccessList, AccessListRule
25
25
```
26
26
@@ -50,13 +50,20 @@ class AccessListTable(NetBoxTable):
50
50
)
51
51
```
52
52
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
+
53
59
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:
54
60
55
61
```python
56
62
classAccessListTable(NetBoxTable):
57
63
name = tables.Column(
58
64
linkify=True
59
65
)
66
+
default_action = ChoiceFieldColumn()
60
67
rule_count = tables.Column()
61
68
62
69
classMeta(NetBoxTable.Meta):
@@ -67,7 +74,7 @@ class AccessListTable(NetBoxTable):
67
74
68
75
### AccessListRuleTable
69
76
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.
71
78
72
79
```python
73
80
classAccessListRuleTable(NetBoxTable):
@@ -77,6 +84,8 @@ class AccessListRuleTable(NetBoxTable):
0 commit comments