Custom Script stuck 'Pending' if Location model is imported #4871

Closed
opened 2025-12-29 19:21:26 +01:00 by adam · 0 comments
Owner

Originally created by @d-k-7 on GitHub (May 4, 2021).

NetBox version

v2.11.2

Python version

3.8

Steps to Reproduce

  1. Take example custom script from docs. Add location import.

from dcim.choices import DeviceStatusChoices, SiteStatusChoices
from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site, Location
from extras.scripts import *


class NewBranchScript(Script):

    class Meta:
        name = "New Branch"
        description = "Provision a new branch site"
        field_order = ['site_name', 'switch_count', 'switch_model']

    site_name = StringVar(
        description="Name of the new site"
    )
    switch_count = IntegerVar(
        description="Number of access switches to create"
    )
    manufacturer = ObjectVar(
        model=Manufacturer,
        required=False
    )
    switch_model = ObjectVar(
        description="Access switch model",
        model=DeviceType,
        display_field='model',
        query_params={
            'manufacturer_id': '$manufacturer'
        }
    )

    def run(self, data, commit):

        # Create the new site
        site = Site(
            name=data['site_name'],
            slug=slugify(data['site_name']),
            status=SiteStatusChoices.STATUS_PLANNED
        )
        site.save()
        self.log_success(f"Created new site: {site}")

        # Create access switches
        switch_role = DeviceRole.objects.get(name='Access Switch')
        for i in range(1, data['switch_count'] + 1):
            switch = Device(
                device_type=data['switch_model'],
                name=f'{site.slug}-switch{i}',
                site=site,
                status=DeviceStatusChoices.STATUS_PLANNED,
                device_role=switch_role
            )
            switch.save()
            self.log_success(f"Created new switch: {switch}")

        # Generate a CSV table of new devices
        output = [
            'name,make,model'
        ]
        for switch in Device.objects.filter(site=site):
            attrs = [
                switch.name,
                switch.device_type.manufacturer.name,
                switch.device_type.model
            ]
            output.append(','.join(attrs))

        return '\n'.join(output)
  1. Execute script

Expected Behavior

The script should have completed running.

Observed Behavior

The job stays stuck in a pending state

Originally created by @d-k-7 on GitHub (May 4, 2021). ### NetBox version v2.11.2 ### Python version 3.8 ### Steps to Reproduce 1. Take example custom script from docs. Add location import. ```from django.utils.text import slugify from dcim.choices import DeviceStatusChoices, SiteStatusChoices from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site, Location from extras.scripts import * class NewBranchScript(Script): class Meta: name = "New Branch" description = "Provision a new branch site" field_order = ['site_name', 'switch_count', 'switch_model'] site_name = StringVar( description="Name of the new site" ) switch_count = IntegerVar( description="Number of access switches to create" ) manufacturer = ObjectVar( model=Manufacturer, required=False ) switch_model = ObjectVar( description="Access switch model", model=DeviceType, display_field='model', query_params={ 'manufacturer_id': '$manufacturer' } ) def run(self, data, commit): # Create the new site site = Site( name=data['site_name'], slug=slugify(data['site_name']), status=SiteStatusChoices.STATUS_PLANNED ) site.save() self.log_success(f"Created new site: {site}") # Create access switches switch_role = DeviceRole.objects.get(name='Access Switch') for i in range(1, data['switch_count'] + 1): switch = Device( device_type=data['switch_model'], name=f'{site.slug}-switch{i}', site=site, status=DeviceStatusChoices.STATUS_PLANNED, device_role=switch_role ) switch.save() self.log_success(f"Created new switch: {switch}") # Generate a CSV table of new devices output = [ 'name,make,model' ] for switch in Device.objects.filter(site=site): attrs = [ switch.name, switch.device_type.manufacturer.name, switch.device_type.model ] output.append(','.join(attrs)) return '\n'.join(output) ``` 2. Execute script ### Expected Behavior The script should have completed running. ### Observed Behavior The job stays stuck in a pending state
adam added the type: bug label 2025-12-29 19:21:26 +01:00
adam closed this issue 2025-12-29 19:21:27 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4871