Hi all,
I'm currently working on a Custom Script that adds new switches to NetBox and configures a number of default values for its interfaces etc.. I'd like to add values to a number of custom fields via this script. One for example is a field that contains the "end of service date". In my custom script I basically have this:
...
from utilities.forms import DatePicker
...
eos_date = StringVar(
description="End of service date",
required=False,
widget=DatePicker()
)
...
This will let me pick a date with the DatePicker. I have a Custom Field "end_of_service" of type "Date" which I want to set. In my custom script I have a switch device and I can see that
switch.get_custom_fields()
will return the end_of_service custom field as described here:
https://github.com/netbox-community/netbox/blob/3d3748d6f54cd2bd168554b141b5f3541a0b88ac/netbox/extras/models/customfields.py#L40
However I can't seem to figure out how to set the value of the custom field. Could you give me a hint? Intuitively I'd probably do something like
for cf in switch.get_custom_fields():
if
cf.name == "....."
and then set the value of the custom field somehow. I'm sorry for posing such clumsy questions. I'm not a django coder, I'm just a network guy who happens to know some python. :)
I have one more question. I'd like to make sure that no invalid input for custom fields is possible in the Custom Sript. I've seen that there's a way to get a suitable form field from a CustomField:
https://github.com/netbox-community/netbox/blob/3d3748d6f54cd2bd168554b141b5f3541a0b88ac/netbox/extras/models/customfields.py#L223
I'm not 100% sure what the difference between a widget and a form field is but I wonder if it makes sense to somehow derive the correct widget type or form type for a custom field via the to_form_field function? (It's very likely that this question makes no sense at all. ;) )
Thanks so much for your help, it's highly appreciated. And thanks for all the work you put into NetBox, it's a fantastic piece of software.
Christian