netbox-community / netbox-plugin-tutorial

A tutorial on building custom plugins for NetBox v3.2+
90 stars 48 forks source link

Issue loading model form in AccessListEditView and AccessListRuleEditView #10

Closed dankotrajkovic closed 2 years ago

dankotrajkovic commented 2 years ago

In the guide the EditView forms are defined as:

class AccessListEditView(generic.ObjectEditView):
    queryset = models.AccessList.objects.all()
    form = forms.AccessListForm

class AccessListRuleEditView(generic.ObjectEditView):
    queryset = models.AccessListRule.objects.all()
    form = forms.AccessListRuleForm

Trying to render the pages an error occur:

TypeError at /plugins/access-lists/access-lists/add/
'NoneType' object is not callable
Request Method: GET
Request URL:    http://127.0.0.1:8000/plugins/access-lists/access-lists/add/
Django Version: 4.0.3
Exception Type: TypeError
Exception Value:    
'NoneType' object is not callable
Exception Location: /opt/netbox/netbox/netbox/views/generic/object_views.py, line 342, in get
Python Executable:  /opt/netbox/venv/bin/python3
Python Version: 3.8.10
Python Path:    
['/opt/netbox/netbox',
 '/usr/lib/python38.zip',
 '/usr/lib/python3.8',
 '/usr/lib/python3.8/lib-dynload',
 '/opt/netbox/venv/lib/python3.8/site-packages',
 '/home/network/netbox-plugin-demo']
Server time:    Tue, 05 Apr 2022 12:43:58 +0000

Checking the inherited class generic.ObjectEditView it seems the variable name for model form should be model_form

Updating the two views in views.py as shown here seems to fix the issue:

class AccessListEditView(generic.ObjectEditView):
    queryset = models.AccessList.objects.all()
-   form = forms.AccessListForm
+   model_form = forms.AccessListForm

class AccessListRuleEditView(generic.ObjectEditView):
    queryset = models.AccessListRule.objects.all()
-   form = forms.AccessListRuleForm
+   model_form = forms.AccessListRuleForm
jeremystretch commented 2 years ago

model_form was changed to form for v3.2.0. This may not have been reflected in the beta releases.