How to interact with Nautobot objects in jobs #3298
-
Hi there This may be a dump question, but I haven't found any information in the developer's guide. For "external" interaction with Nautobot, the REST APIs are clearly the way to go. What is the intended way to interact with Nautobot objects like devices, interfaces, VLAN objects in Nautobot jobs? Is it ok to CRUD objects the "Django way" with create/delete/get/filter/save or is this a very bad practice?
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
This is absolutely okay, but you can simplify your example further, too:
You only need Q if you plan on more complex filtering operations such as or/and filtering between different filters - see here for more information. |
Beta Was this translation helpful? Give feedback.
-
Is there something missing from the Jobs documentation? There are example Jobs provided towards the bottom. As you can see in the examples, you're absolutely welcome to use the standard Django ORM methods such as create, delete, etc. It would actually be advised to use those methods as you will have the best performance when interacting with the backend database using them. The one thing to be aware of though is that it's advised to use the |
Beta Was this translation helpful? Give feedback.
-
Hi, Wow, that was very quick. Thanks for your answers and suggestions :-) I did consult the Jobs wiki page, but I didn't actually find an example that called a Nautobot object class. This made me suspicious. I have already developed a few jobs that update Nautobot objects using the standard Django ORM methods and saving them with __getUniqueVlanObj(self=self, device=device, all_nautobot_vlans=all_nautobot_vlans,
vlanid=int(device_phpipam_ip["vlaninfo"]["number"]))
nautobot_ip_interface = Interface.objects.create(
device=device,
name=ip_interface_name,
type=InterfaceTypeChoices.TYPE_VIRTUAL,
status=status_active,
label=ip_interface_name,
mgmt_only=mgmt_only,
enabled=True,
untagged_vlan=untagged_vlan,
mode = "access"
) or __getUniqueVlanObj(self=self, device=device, all_nautobot_vlans=all_nautobot_vlans,
vlanid=int(device_phpipam_ip["vlaninfo"]["number"]))
nautobot_ip_interface = Interface(
device=device,
name=ip_interface_name,
type=InterfaceTypeChoices.TYPE_VIRTUAL,
status=status_active,
label=ip_interface_name,
mgmt_only=mgmt_only,
enabled=True,
untagged_vlan=untagged_vlan,
mode = "access"
)
nautobot_ip_interface.validated_save() I noticed this because I was missing the check for the existence of a certain custom field in my custom transposer. I had to run Have I missed something or is this expected behaviour? |
Beta Was this translation helpful? Give feedback.
Is there something missing from the Jobs documentation? There are example Jobs provided towards the bottom. As you can see in the examples, you're absolutely welcome to use the standard Django ORM methods such as create, delete, etc. It would actually be advised to use those methods as you will have the best performance when interacting with the backend database using them. The one thing to be aware of though is that it's advised to use the
validated_save()
function when saving objects instead of the typical Djangosave()
method. This enables validation of the object at creation time to ensure it's being created correctly and isn't missing any field data.