Custom Script: Set values for custom fields

1,096 views
Skip to first unread message

Christian Strauf

unread,
Feb 17, 2021, 9:34:02 AM2/17/21
to NetBox
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

Brian Candler

unread,
Feb 17, 2021, 11:32:37 AM2/17/21
to NetBox
Netbox 2.10?  It's simpler than that: it's just a JSON field on your model.

switch.custom_field_data["foo"] = "bar"

Look for CustomFieldModel in netbox/extras/models/customfields.py

As for widgets which pre-validate inputs: sorry I can't answer that.  Of course, your custom script can do its own validation on the data received from the form.

Christian Strauf

unread,
Feb 18, 2021, 12:40:07 AM2/18/21
to Brian Candler, NetBox
Hi Brian!

> Netbox 2.10? It's simpler than that: it's just a JSON field on your model.
>
> switch.custom_field_data["foo"] = "bar"
>
> Look for CustomFieldModel in netbox/extras/models/customfields.py
That's fantastic, that's what I hoped for. Yes, we're on 2.10. I'll give it a try today. Thanks!

Christian

Reply all
Reply to author
Forward
0 new messages