pynetbox: Updating object with JSON custom field causes a traceback

I have a JSON custom field on some objects in my NetBox 3.2beta instance. When trying to update one of the objects I get this traceback.

Traceback (most recent call last):
  File "/home/jcollie/dev/netbox-maintenance/fix-model-aliases.py", line 602, in <module>
    main()
  File "/usr/lib/python3.10/site-packages/click/core.py", line 1137, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3.10/site-packages/click/core.py", line 1062, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3.10/site-packages/click/core.py", line 763, in invoke
    return __callback(*args, **kwargs)
  File "/home/jcollie/dev/netbox-maintenance/fix-model-aliases.py", line 597, in main
    if device_type.save():
  File "/home/jcollie/.local/lib/python3.10/site-packages/pynetbox/core/response.py", line 529, in save
    updates = self.updates()
  File "/home/jcollie/.local/lib/python3.10/site-packages/pynetbox/core/response.py", line 506, in updates
    diff = self._diff()
  File "/home/jcollie/.local/lib/python3.10/site-packages/pynetbox/core/response.py", line 484, in _diff
    {fmt_dict(k, v) for k, v in self.serialize(init=True).items()}
  File "/home/jcollie/.local/lib/python3.10/site-packages/pynetbox/core/response.py", line 457, in serialize
    ret[i] = flatten_custom(current_val)
  File "/home/jcollie/.local/lib/python3.10/site-packages/pynetbox/core/response.py", line 61, in flatten_custom
    return {
  File "/home/jcollie/.local/lib/python3.10/site-packages/pynetbox/core/response.py", line 62, in <dictcomp>
    k: v if not isinstance(v, dict) else v["value"] for k, v in custom_dict.items()
KeyError: 'value'

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 8
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Thanks @rodvand. I’ve tested the solution proposed above by markkuleinio and now everything works. Not sure if it may break something else, but up to the moment all other automations I’ve works fine.

It will be really nice if this can be included in pynetbox production release.

Any workaround for this that you know of? I am stuck at updating our objects now when we use custom_fields as objects.

– Kind Regards Falk

I’ve forked the repository and did a small change https://github.com/Kani999/pynetbox/tree/fix_cf_6.6.2

Then I released the forked version to pypi, so I can simply install it in my projects.

I’m trying to maintain the version with the original pynetbox, but I don’t guarantee anything in the future

I’m trying to maintain the version with the original pynetbox, but I don’t guarantee anything in the future

Thanks, going to use is for the moment, until you want to deprecate or stale it!

– Kind Regards Falk

Hi, I’ve started using custom fields of type objects on my NetBox instance. The flatten_custom() is definitely needed to allow pynetbox to update such records, but I had to modify it like this: k: v if not isinstance(v, dict) else v["id"] for k, v in custom_dict.items() I am not sure either in which case we could encounter a Dict with a “value” key in a custom field, but the problem with the solution above is that it probably breaks things again for json fields. I didn’t look further yet but I think we would need to look at the custom field type in order to flatten it properly? Or not at all if it’s a JSON. Edit: We probably can’t have this information from the API. So a more elaborate flatten_custom() would be needed with key id and value not allowed in JSON custom fields? k: v if not isinstance(v, dict) or not v.get("id") else v["id"] for k, v in custom_dict.items() Edit2: We can get the field type from /extras/custom-fields/ but that requires extra API calls. Options were already discussed here https://github.com/netbox-community/pynetbox/issues/436#issuecomment-1025140828

But flatten_custom() is only used for custom fields, see the line #456 in the snippet above thinking

Not saying it makes sense, just trying to guess what the author may have been thinking when they wrote that particular piece of code.