Inherit Script class #17040
-
Hi, Example: """base.py."""
from extras.scripts import BooleanVar, Script
class CustomScript(Script):
nice_flag = BooleanVar("Activate nice flag", default=False)
def common_function(self):
self.log_success("You're beautiful") """myscript.py"""
from scripts.base import CustomScript
class BestScript(CustomScript):
def run(self, data, commit):
self.common_function() When i add myscript.py this is the result: Any suggestion to avoid this behavior? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Try using a Mixin instead of subclassing.
Plenty of examples of this in the Netbox core code - just search for the text |
Beta Was this translation helpful? Give feedback.
-
Seems to work, thank you! Final example: """base.py."""
from extras.scripts import BooleanVar
class CustomScriptMixin:
class Meta: # noqa: D106
commit_default = False
fieldsets = (
(
"Nice section",
("nice_flag",),
),
)
nice_flag = BooleanVar("Activate nice flag", default=False)
def common_function(self):
self.log_success("You're beautiful") """myscript.py"""
from extras.scripts import Script
from scripts.base_script import CustomScriptMixin
class BestScript(Script, CustomScriptMixin):
def run(self, data, commit):
self.common_function() |
Beta Was this translation helpful? Give feedback.
Try using a Mixin instead of subclassing.
Plenty of examples of this in the Netbox core code - just search for the text
Mixin
. I'm not sure ifnice_flag
would be picked up this way though, and if not, you might need to use a subclassing hook.