I have a custom field called "Warranty Type" on the "Device" model.
root@netbox:~# cd /opt/netbox/netbox/
root@netbox:/opt/netbox/netbox# python3 manage.py nbshell
### NetBox interactive shell (netbox)
### Python 3.6.9 | Django 2.2.8 | NetBox 2.6.9
### lsmodels() will show available models. Use help(<model>) for more info.
>>> from extras.models import ContentType
>>> ct = ContentType.objects.get_for_model(Device)
>>> ct
<ContentType: device>
>>> cf = CustomField.objects.get(obj_type=ct, name="Warranty Type")
>>> cf
<CustomField: Warranty type>
>>> cf.type
600
This is CF_TYPE_SELECT from netbox/extras/constants.py, so the available selections for this field are also defined in the database:
>>> CustomFieldChoice.objects.filter(field=cf)
<QuerySet [<CustomFieldChoice: Stortrec NBD>, <CustomFieldChoice: Dell Basic>, <CustomFieldChoice: Dell ProSupport>, ...
>>> ch = CustomFieldChoice.objects.get(field=cf, value="Apple Care Plus")
>>>
Finally, I want to set this on a particular device:
>>> d = Device.objects.get(name="DAS03")
>>> CustomFieldValue.objects.update_or_create(field=cf, obj_type=ct, obj_id=d.id, serialized_value=ch.id) (<CustomFieldValue: DAS03 Warranty type>, True)
>>>
>>> d.cf["Warranty Type"] <CustomFieldChoice: Apple Care Plus>
If you're using text custom fields, then I think the serialized_value is just the text, which makes it simpler.