Unable to import the dcim module from a custom management command #203

Closed
opened 2025-12-29 16:19:18 +01:00 by adam · 3 comments
Owner

Originally created by @Zanthras on GitHub (Jul 13, 2016).

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.

Originally created by @Zanthras on GitHub (Jul 13, 2016). 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.
adam closed this issue 2025-12-29 16:19:18 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jul 13, 2016):

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 commented on GitHub (Jul 13, 2016): 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.
Author
Owner

@Zanthras commented on GitHub (Jul 13, 2016):

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 commented on GitHub (Jul 13, 2016): ``` 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.
Author
Owner

@Zanthras commented on GitHub (Jul 13, 2016):

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

@Zanthras commented on GitHub (Jul 13, 2016): Well thats both annoying and easier, it does just work after an update. I probably futz'ed something up at some point.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#203