Site(tags) are not created when creating a Site using NetBox scripts function #3508

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

Originally created by @ryanmerolle on GitHub (Mar 26, 2020).

Environment

  • Python version: 3.6.8
  • NetBox version: 2.7.10

Steps to Reproduce

Using the below script in the scripts directory for netbox & entering a new site name, creates the site, but without tags.

from django.utils.text import slugify
from dcim.models import Site
from extras.scripts import *

class NewColoScript(Script):

    class Meta:
        name = "New Colo"
        description = "Provision a new colo site"
        field_order = ['site_name']

    site_name = StringVar(
        label="Site Name",
        description="Site Code Per the Colo Provider",
        required=True
    )

    def run(self, data, commit):

        # Create the new site
        site = Site(
            name=data['site_name'].upper(),
            slug=slugify(data['site_name']),
            tags=["colo", "test"]
        )
        site.save()
        self.log_success("Created new site: {}".format(site))

I have even tried by pre-creating the tags and referencing the name(s) as well as referencing the tag id.

Expected Behavior

Create the site and populate tags.

Observed Behavior

Create the site without tags.

Originally created by @ryanmerolle on GitHub (Mar 26, 2020). ### Environment * Python version: 3.6.8 * NetBox version: 2.7.10 ### Steps to Reproduce Using the below script in the scripts directory for netbox & entering a new site name, creates the site, but without tags. ```python from django.utils.text import slugify from dcim.models import Site from extras.scripts import * class NewColoScript(Script): class Meta: name = "New Colo" description = "Provision a new colo site" field_order = ['site_name'] site_name = StringVar( label="Site Name", description="Site Code Per the Colo Provider", required=True ) def run(self, data, commit): # Create the new site site = Site( name=data['site_name'].upper(), slug=slugify(data['site_name']), tags=["colo", "test"] ) site.save() self.log_success("Created new site: {}".format(site)) ``` I have even tried by pre-creating the tags and referencing the name(s) as well as referencing the tag id. ### Expected Behavior Create the site and populate tags. ### Observed Behavior Create the site without tags.
adam closed this issue 2025-12-29 18:29:34 +01:00
Author
Owner

@jeremystretch commented on GitHub (Mar 26, 2020):

Please see the django-taggit documentation for direction on assigning tags to objects in Python. The native mechanism works differently than the REST API.

@jeremystretch commented on GitHub (Mar 26, 2020): Please see the [django-taggit documentation](https://github.com/jazzband/django-taggit) for direction on assigning tags to objects in Python. The native mechanism works differently than the REST API.
Author
Owner

@ryanmerolle commented on GitHub (Mar 26, 2020):

Thanks @jeremystretch apologies for bothering you on this, but it was not clear from the docs.

I will submit a PR to add this to the scripts section of the docs.

For anyone who stumbles upon this the fix is by implementing:

from django.utils.text import slugify
from dcim.models import Site
from extras.scripts import *

class NewColoScript(Script):

    class Meta:
        name = "New Colo"
        description = "Provision a new colo site"
        field_order = ['site_name']

    site_name = StringVar(
        label="Site Name",
        description="Site Code Per the Colo Provider",
        required=True
    )

    def run(self, data, commit):

        # Create the new site
        site = Site(
            name=data['site_name'].upper(),
            slug=slugify(data['site_name'])
            # Removed tags line
        )
        site.save()
        site.tags.add("colo", "test") # taggit add set here, after the site is saved, because a primary key needs to be set for the site before tags can be added.
        self.log_success("Created new site: {}".format(site))
@ryanmerolle commented on GitHub (Mar 26, 2020): Thanks @jeremystretch apologies for bothering you on this, but it was not clear from the docs. I will submit a PR to add this to the scripts section of the docs. For anyone who stumbles upon this the fix is by implementing: ```python from django.utils.text import slugify from dcim.models import Site from extras.scripts import * class NewColoScript(Script): class Meta: name = "New Colo" description = "Provision a new colo site" field_order = ['site_name'] site_name = StringVar( label="Site Name", description="Site Code Per the Colo Provider", required=True ) def run(self, data, commit): # Create the new site site = Site( name=data['site_name'].upper(), slug=slugify(data['site_name']) # Removed tags line ) site.save() site.tags.add("colo", "test") # taggit add set here, after the site is saved, because a primary key needs to be set for the site before tags can be added. self.log_success("Created new site: {}".format(site)) ```
Author
Owner

@jeremystretch commented on GitHub (Mar 26, 2020):

This is out of scope for the documentation on custom scripts, which deals only with the creation of a script itself. Script authors are responsible for researching and implementing all logic within the run() method.

@jeremystretch commented on GitHub (Mar 26, 2020): This is out of scope for the documentation on custom scripts, which deals only with the creation of a script itself. Script authors are responsible for researching and implementing all logic within the `run()` method.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3508