netbox plugin model problem

390 views
Skip to first unread message

Tommy Xu

unread,
Jul 16, 2020, 4:47:11 PM7/16/20
to NetBox
Hello, I am trying to develop a plugin for netbox but I having having troubles with migrating the models.py. I am using netbox-docker and I currently having my Django project inside the netbox-docker directory while my Django App (tommy_one) which is this plugin is inside the Djando project and is working fine on my local deployment of netbox-docker.

However, I have a few questions about making new models and migrating them in the Django app. I have my example init file here and the from extras.plugins import is making an error that I have no idea how to solve as a simple pip install extras didn't work. Which this error, migrations using manage.py (that is inside the Django project dir) is failing when trying to update the models.

from extras.plugins import PluginConfig


class TommyPluginConfig(PluginConfig):
name = 'tommy_plugin'
verbose_name = 'Tommy Plugin'
description = 'An example plugin for development purposes'
version = '0.1'
author = 'Tommy Xu'
author_email = 'aut...@example.com'
base_url = 'tommy-plugin'
required_settings = []
default_settings = {
'loud': False
}


config = TommyPluginConfig

If I were to comment out the init and continue, I have something like this in my first few lines of models.py using what is provided in the onboarding example plugin. 
from django.db import models

class Netbox_Devices(models.Model):

    created_device = models.ForeignKey(to="dcim.Device", on_delete=models.SET_NULL, blank=True, null=True)

    ip_address = models.CharField(max_length=255, help_text="primary ip address for the device", null=True)

    site = models.ForeignKey(to="dcim.Site", on_delete=models.SET_NULL, blank=True, null=True)


However, when using manage.py the first few lines of error:
tommy_one.Netbox_Devices.created_device: (fields.E300) Field defines a relation with model 'dcim.Device', which is either not installed, or is abstract.
tommy_one.Netbox_Devices.created_device: (fields.E307) The field tommy_one.Netbox_Devices.created_device was declared with a lazy reference to 'dcim.device', but app 'dcim' isn't installed.
tommy_one.Netbox_Devices.platform: (fields.E300) Field defines a relation with model 'dcim.Platform', which is either not installed, or is abstract.

I am pretty sure I am missing something here as I can see my devices on my local deployment of netbox-docker fine after the database restore, just I am not sure what I need to put for my plugin to recognize them. 

Oz Oid

unread,
Jul 23, 2020, 11:29:51 AM7/23/20
to NetBox
I think you need to reference the Device Object rather than the "dcim.device" - below is what i am using (although having issues with migrate).

from django.db import models
from dcim.models import Device

class DeviceChangeRequest(models.Model):
    nbdevice_id = models.ForeignKey(Device,on_delete=models.SET_NULL,blank=Truenull=True)
    ubdevice_id = models.CharField(max_length=50)
    subject = models.CharField(max_length=50)
    rtype = models.CharField(max_length=100)
    request = models.TextField()
    ticket_id = models.CharField(max_length=20)
    
    def __str__(self):
        return self.subject
Reply all
Reply to author
Forward
0 new messages