'VMInterface' object has no attribute 'get' #10886

Closed
opened 2025-12-29 21:37:16 +01:00 by adam · 6 comments
Owner

Originally created by @variable on GitHub (Mar 12, 2025).

Originally assigned to: @jnovinger on GitHub.

Deployment Type

Self-hosted

NetBox Version

4.2.2

Python Version

3.11

Steps to Reproduce

I am trying to associate a VM interface to a prefix via custom field, using Pynetbox

new_prefix.custom_fields['workflow_associated_prefix_to_interface_mapping'] = nb_interface.id
new_prefix.save()

This is the custom field

Image

If I comment out the custom_field statement, save is fine. I also tried saving via UI with the relationship, UI worked fine.

Expected Behavior

Saving works.

Observed Behavior

And I got this error

pynetbox.core.query.RequestError: The request failed with code 500 Internal Server Error: {'error': "'VMInterface' object has no attribute 'get'", 'exception': 'AttributeError', 'netbox_version': '4.2.2', 'python_version': '3.11.2'}

It appears this is the line that threw the error:

Image
def validate(self, data):
        # Validate many-to-many VLAN assignments
        virtual_machine = self.instance.virtual_machine if self.instance else data.get('virtual_machine')  <-----
        for vlan in data.get('tagged_vlans', []):
            if vlan.site not in [virtual_machine.site, None]:
                raise serializers.ValidationError({
                    'tagged_vlans': f"VLAN {vlan} must belong to the same site as the interface's parent virtual "
                                    f"machine, or it must be global."
                })

Somehow... data is already a VMInterface object

Originally created by @variable on GitHub (Mar 12, 2025). Originally assigned to: @jnovinger on GitHub. ### Deployment Type Self-hosted ### NetBox Version 4.2.2 ### Python Version 3.11 ### Steps to Reproduce I am trying to associate a VM interface to a prefix via custom field, using Pynetbox ```python new_prefix.custom_fields['workflow_associated_prefix_to_interface_mapping'] = nb_interface.id new_prefix.save() ``` This is the custom field ![Image](https://github.com/user-attachments/assets/5f52bba8-e55c-4c27-adc0-59d08a5866b5) If I comment out the custom_field statement, save is fine. I also tried saving via UI with the relationship, UI worked fine. ### Expected Behavior Saving works. ### Observed Behavior And I got this error ``` pynetbox.core.query.RequestError: The request failed with code 500 Internal Server Error: {'error': "'VMInterface' object has no attribute 'get'", 'exception': 'AttributeError', 'netbox_version': '4.2.2', 'python_version': '3.11.2'} ``` It appears this is the line that threw the error: <img width="1319" alt="Image" src="https://github.com/user-attachments/assets/4f99eb14-b4c7-40e2-bbe7-3307f28e11a0" /> ```python def validate(self, data): # Validate many-to-many VLAN assignments virtual_machine = self.instance.virtual_machine if self.instance else data.get('virtual_machine') <----- for vlan in data.get('tagged_vlans', []): if vlan.site not in [virtual_machine.site, None]: raise serializers.ValidationError({ 'tagged_vlans': f"VLAN {vlan} must belong to the same site as the interface's parent virtual " f"machine, or it must be global." }) ``` Somehow... data is already a VMInterface object
adam added the type: bugstatus: acceptedseverity: medium labels 2025-12-29 21:37:16 +01:00
adam closed this issue 2025-12-29 21:37:17 +01:00
Author
Owner

@jnovinger commented on GitHub (Mar 13, 2025):

@variable , I think you've confused which direction your custom field is relating things. You write:

new_prefix.custom_fields['workflow_associated_prefix_to_interface_mapping'] = nb_interface.id
new_prefix.save()

This indicates that you're trying set a custom field on an instance of Prefix. However, your screenshot of the configured custom field shows that the workflow_associated_prefix_to_interface_mapping custom field is attached to the VMInterface model. Therefore your assignment should look something like:

In [1]: import pynetbox

In [2]: api_key = '7c9f778294daca657d14cd87d63923480ad38a62'

In [3]: nb = pynetbox.api('http://127.0.0.1:8000', token=api_key)

In [4]: interfaces = list(nb.virtualization.interfaces.all())

In [5]: vmi0 = interfaces[0]

In [6]: vmi0.custom_fields
Out[6]: {'workflow_associated_prefix_to_interface_mapping': None}

In [7]: p0 = nb.ipam.prefixes.get(id=1)

In [8]: vmi0.custom_fields['workflow_associated_prefix_to_interface_mapping'] = p0.id

In [9]: vmi0.custom_fields
Out[9]: {'workflow_associated_prefix_to_interface_mapping': 1}

In [10]: vmi0.updates()
Out[10]: {'custom_fields': {'workflow_associated_prefix_to_interface_mapping': 1}}

In [11]: vmi0.save()
Out[11]: True

This is behaving as intended.

@jnovinger commented on GitHub (Mar 13, 2025): @variable , I think you've confused which direction your custom field is relating things. You write: ```python new_prefix.custom_fields['workflow_associated_prefix_to_interface_mapping'] = nb_interface.id new_prefix.save() ``` This indicates that you're trying set a custom field on an instance of `Prefix`. However, your screenshot of the configured custom field shows that the `workflow_associated_prefix_to_interface_mapping` custom field is attached to the `VMInterface` model. Therefore your assignment should look something like: ```python In [1]: import pynetbox In [2]: api_key = '7c9f778294daca657d14cd87d63923480ad38a62' In [3]: nb = pynetbox.api('http://127.0.0.1:8000', token=api_key) In [4]: interfaces = list(nb.virtualization.interfaces.all()) In [5]: vmi0 = interfaces[0] In [6]: vmi0.custom_fields Out[6]: {'workflow_associated_prefix_to_interface_mapping': None} In [7]: p0 = nb.ipam.prefixes.get(id=1) In [8]: vmi0.custom_fields['workflow_associated_prefix_to_interface_mapping'] = p0.id In [9]: vmi0.custom_fields Out[9]: {'workflow_associated_prefix_to_interface_mapping': 1} In [10]: vmi0.updates() Out[10]: {'custom_fields': {'workflow_associated_prefix_to_interface_mapping': 1}} In [11]: vmi0.save() Out[11]: True ``` This is behaving as intended.
Author
Owner

@srigby345s commented on GitHub (Mar 14, 2025):

@jnovinger Is the image not showing what was described? The object type on the right hand side is IPAM | prefix and the type of the linked object is Vminterface.

I am having the same issue currently while using custom fields to link one vminterface to another which was working just fine on 3.4 but has been failing since updating to 4.2.4 last week.

Image

I tested directly via the swagger-ui page on my local netbox setup.

Image

@srigby345s commented on GitHub (Mar 14, 2025): @jnovinger Is the image not showing what was described? The object type on the right hand side is IPAM | prefix and the type of the linked object is Vminterface. I am having the same issue currently while using custom fields to link one vminterface to another which was working just fine on 3.4 but has been failing since updating to 4.2.4 last week. ![Image](https://github.com/user-attachments/assets/25557a45-09a8-4d87-9d53-3afee15f3c60) I tested directly via the swagger-ui page on my local netbox setup. ![Image](https://github.com/user-attachments/assets/a1ebf2b4-4c42-44ad-b879-421f6a93d1c8)
Author
Owner

@jnovinger commented on GitHub (Mar 14, 2025):

Oof, you are correct @srigby345s . I was definitely the one misreading things in this case. Thanks for keeping me honest!

I'm going to re-open this issue and put it back in triage, with the intention of revisiting it later today.

@jnovinger commented on GitHub (Mar 14, 2025): Oof, you are correct @srigby345s . I was definitely the one misreading things in this case. Thanks for keeping me honest! I'm going to re-open this issue and put it back in triage, with the intention of revisiting it later today.
Author
Owner

@jnovinger commented on GitHub (Mar 14, 2025):

@variable , my apologies for misunderstanding the bug report!

@jnovinger commented on GitHub (Mar 14, 2025): @variable , my apologies for misunderstanding the bug report!
Author
Owner

@variable commented on GitHub (Mar 17, 2025):

@variable , my apologies for misunderstanding the bug report!

No worries!

@variable commented on GitHub (Mar 17, 2025): > [@variable](https://github.com/variable) , my apologies for misunderstanding the bug report! No worries!
Author
Owner

@variable commented on GitHub (Mar 22, 2025):

Thanks for fixing the issue guys.

@variable commented on GitHub (Mar 22, 2025): Thanks for fixing the issue guys.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#10886