"custom_fields" are the definitions of which fields have been defined for this particular type of object, while "custom_field_values" are actual user-entered values for a given custom_field. Hence you'll need to manipulate the CustomFieldValue model - in your case, you would *delete* items to reset them to unset (default).
On the Device end, the relation you want is called "custom_field_values":
custom_field_values = GenericRelation(
to='extras.CustomFieldValue',
content_type_field='obj_type',
object_id_field='obj_id'
)
Also look at class CustomFieldValue(models.Model) in netbox/extras/models.py. It's a
GenericForeignKey relation to object.
If you're only reading custom field values, there's a convenience helper method "cf" which collects all the values into a dict. But this code may also give you some clues as to how to access them.
def cache_custom_fields(self):
"""
Cache all custom field values for this instance
"""
self._cf = {
field.name: value for field, value in self.get_custom_fields().items() }
@property
def cf(self):
"""
Name-based CustomFieldValue accessor for use in templates
"""
if self._cf is None:
self.cache_custom_fields()
return self._cf
def get_custom_fields(self):
"""
Return a dictionary of custom fields for a single object in the form {<field>: value}.
"""
# Find all custom fields applicable to this type of object
content_type = ContentType.objects.get_for_model(self)
fields = CustomField.objects.filter(obj_type=content_type)
# If the object exists, populate its custom fields with values
if hasattr(self, 'pk'):
values = self.custom_field_values.all()
values_dict = {cfv.field_id: cfv.value for cfv in values}
return OrderedDict([(field, values_dict.get(field.pk)) for field in fields]) else:
return OrderedDict([(field, None) for field in fields])