KeyError with custom_field_data on plugin #15447
-
KeyError: 'custom_field_data' When Adding VideoIPathNMOSMultiDeviceConfigurationEnvironment
DescriptionI'm developing a NetBox plugin and encountered a Steps to Reproduce
Expected BehaviorI expected the form to save the new Actual BehaviorThe server throws a Traceback (Most Relevant Call Last)File "/opt/netbox-3.7.3/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner Additional InformationI suspect the issue might be related to how custom fields are serialized or perhaps a mismatch in expectations between the plugin and NetBox's core functionalities regarding custom fields. I've looked into the NetBox documentation and source code but haven't found a clear indication of what might be causing this issue. The error I am getting only shows up on an inherited model. Does this maybe have to do something with my error? Any insights, suggestions, or guidance on how to resolve this error would be greatly appreciated. Thank you in advance for your time and help. Here are some code snippets from the source code:models.py:
forms.py:
views.py:
api/serializers.py:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
You explicitly asked the serializer to include custom_fields (ditto for VideoIPathDeviceConfigurationSerializer). But your model does not inherit from CustomFieldsMixin |
Beta Was this translation helpful? Give feedback.
-
Thanks for your help. A Mixin class did the job! |
Beta Was this translation helpful? Give feedback.
-
Hi, I am working as a plugin developer too and ran into the same exact issue. If I understood correctly, Netbox doesn't block using multi-table inheritance but it is not the recommended way to go? More spesifically: I would have to create a Mixin class to handle the 'custom_field_data' serializer issue on the model that is inheriting from the parent class that inherits from NetboxModel? |
Beta Was this translation helpful? Give feedback.
Then I suspect it's because you've subclassed VideoIPathNMOSMultiDeviceConfiguration from VideoIPathDeviceConfiguration.
This isn't something you'd normally do in Django. For a start, which table will each type of object reside in? Either you will have multi-table inheritance, which is generally a bad thing, or you're trying to do some sort of single-table polymorphism which probably requires a plugin.
You can see that Netbox itself uses mixins, or abstract base classes: if you're looking to share code or attributes between two models, that would be the best way to go about it IMO.