Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
variable opened this issue Mar 12, 2025 · 5 comments · Fixed by #18945
Closed

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

variable opened this issue Mar 12, 2025 · 5 comments · Fixed by #18945
Assignees
Labels
severity: medium Results in substantial degraded or broken functionality for specfic workflows status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@variable
Copy link

variable commented Mar 12, 2025

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

@variable variable added status: needs triage This issue is awaiting triage by a maintainer type: bug A confirmed report of unexpected behavior in the application labels Mar 12, 2025
@jnovinger
Copy link
Contributor

jnovinger commented 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 jnovinger closed this as not planned Won't fix, can't repro, duplicate, stale Mar 13, 2025
@srigby345s
Copy link

@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

@jnovinger
Copy link
Contributor

jnovinger commented 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 jnovinger reopened this Mar 14, 2025
@jnovinger jnovinger added severity: medium Results in substantial degraded or broken functionality for specfic workflows status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation labels Mar 14, 2025 — with Linear
@jnovinger jnovinger removed the status: needs triage This issue is awaiting triage by a maintainer label Mar 14, 2025
@jnovinger
Copy link
Contributor

@variable , my apologies for misunderstanding the bug report!

@variable
Copy link
Author

@variable , my apologies for misunderstanding the bug report!

No worries!

@jnovinger jnovinger self-assigned this Mar 17, 2025
@jnovinger jnovinger added status: accepted This issue has been accepted for implementation and removed status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation labels Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity: medium Results in substantial degraded or broken functionality for specfic workflows status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants