netbox icon indicating copy to clipboard operation
netbox copied to clipboard

Unable to import the dcim module from a custom management command

Open Zanthras opened this issue 10 years ago • 3 comments

While adding some custom integrations I found I was unable to import the dcim module.

create a dcim/management/commands script that contains

from dcim import models

This will fail.

If you do the same in the circuits module (and try to import from circuits) this will succeed.

Zanthras avatar Jul 13 '16 06:07 Zanthras

This works fine for me:

from django.core.management.base import BaseCommand
from dcim import models

class Command(BaseCommand):
    help = "Prints device count"

    def handle(self, *args, **options):
        device_count = models.Device.objects.count()
        print("There are {} devices".format(device_count))

Check that you have an __init__.py in both the management and commands directories. Also verify your virtualenv if using one.

jeremystretch avatar Jul 13 '16 13:07 jeremystretch

buntu@netbox1:~$ cd /opt/netbox/netbox/dcim/management/
ubuntu@netbox1:/opt/netbox/netbox/dcim/management$ ll
total 16
drwxr-xr-x 3 root root 4096 Jul 11 23:13 ./
drwxr-xr-x 7 root root 4096 Jul 13 06:40 ../
drwxr-xr-x 2 root root 4096 Jul 13 06:12 commands/
-rw-r--r-- 1 root root    0 Jul 11 23:12 __init__.py
-rw-r--r-- 1 root root  133 Jul 11 23:13 __init__.pyc
ubuntu@netbox1:/opt/netbox/netbox/dcim/management$ cd commands/
ubuntu@netbox1:/opt/netbox/netbox/dcim/management/commands$ ll
total 28
drwxr-xr-x 2 root   root   4096 Jul 13 06:12 ./
drwxr-xr-x 3 root   root   4096 Jul 11 23:13 ../
-rw-r--r-- 1 root   root      0 Jul 11 23:07 __init__.py
-rw-r--r-- 1 root   root    142 Jul 11 23:13 __init__.pyc
-rw-rw-r-- 1 root root 5469 Jul 13 06:44 serverupdate.py

Unless my eyes are decieving me I do, however since its working for you, I must have some other problem, I'll update the project with git and retest to try and figure out where it is.

Zanthras avatar Jul 13 '16 14:07 Zanthras

Well thats both annoying and easier, it does just work after an update. I probably futz'ed something up at some point.

Zanthras avatar Jul 13 '16 15:07 Zanthras