Unable to query devices with primary-ip set in GraphQL - "Cannot resolve keyword 'assigned_object_id' into field" #11669

Closed
opened 2025-12-29 21:48:19 +01:00 by adam · 2 comments
Owner

Originally created by @dxks on GitHub (Oct 2, 2025).

Originally assigned to: @pheus on GitHub.

NetBox Edition

NetBox Community

NetBox Version

v4.4.1

Python Version

3.12

Steps to Reproduce

  1. Create a simple GraphQL-query, i.e.
    query { device_list(filters:{primary_ip4:{assigned:true}}) { id primary_ip4 { assigned_object {__typename} } } }

  2. encounter the error:
    { "data": null, "errors": [ { "message": "Cannot resolve keyword 'assigned_object_id' into field. Choices are: airflow, asset_tag, bgpsession, bookmarks, cabletermination, cluster, cluster_id, comments, config_template, config_template_id, console_port_count, console_server_port_count, consoleports, consoleserverports, contacts, created, custom_field_data, description, designated_arp_router_peering_services, device_bay_count, device_type, device_type_id, devicebays, face, front_port_count, frontports, id, images, interface_count, interfaces, internet_exchange_points, inventory_item_count, inventoryitems, journal_entries, last_updated, latitude, local_context_data, location, location_id, longitude, module_bay_count, modulebays, modules, name, oob_ip, oob_ip_id, parent_bay, peering_services, platform, platform_id, position, power_outlet_count, power_port_count, poweroutlets, powerports, primary_ip4, primary_ip4_id, primary_ip6, primary_ip6_id, rack, rack_id, rear_port_count, rearports, role, role_id, sbfd_discriminator, serial, services, site, site_id, status, subscriptions, tagged_items, tags, tenant, tenant_id, termination_a_device, termination_z_device, vc_master_for, vc_position, vc_priority, vdcs, virtual_chassis, virtual_chassis_id, virtual_machines", "locations": [ { "line": 2, "column": 3 } ], "path": [ "device_list" ] } ] }

Expected Behavior

As the filter has_primary_ip is no longer available (like cabled within InterfaceFilter) I tried the assigned-filter like I do for the connected as a replacement for cabled.

I expected that this would work in the same way and provides a list of devices with a primary ip set.

Observed Behavior

Unfortunately the assigned-filter is applied to the device instead of the Interface and causes the error that it cannot be resolved to a field - as the assigned_object_id is on the interface, not the device.

Originally created by @dxks on GitHub (Oct 2, 2025). Originally assigned to: @pheus on GitHub. ### NetBox Edition NetBox Community ### NetBox Version v4.4.1 ### Python Version 3.12 ### Steps to Reproduce 1. Create a simple GraphQL-query, i.e. `query { device_list(filters:{primary_ip4:{assigned:true}}) { id primary_ip4 { assigned_object {__typename} } } }` 2. encounter the error: `{ "data": null, "errors": [ { "message": "Cannot resolve keyword 'assigned_object_id' into field. Choices are: airflow, asset_tag, bgpsession, bookmarks, cabletermination, cluster, cluster_id, comments, config_template, config_template_id, console_port_count, console_server_port_count, consoleports, consoleserverports, contacts, created, custom_field_data, description, designated_arp_router_peering_services, device_bay_count, device_type, device_type_id, devicebays, face, front_port_count, frontports, id, images, interface_count, interfaces, internet_exchange_points, inventory_item_count, inventoryitems, journal_entries, last_updated, latitude, local_context_data, location, location_id, longitude, module_bay_count, modulebays, modules, name, oob_ip, oob_ip_id, parent_bay, peering_services, platform, platform_id, position, power_outlet_count, power_port_count, poweroutlets, powerports, primary_ip4, primary_ip4_id, primary_ip6, primary_ip6_id, rack, rack_id, rear_port_count, rearports, role, role_id, sbfd_discriminator, serial, services, site, site_id, status, subscriptions, tagged_items, tags, tenant, tenant_id, termination_a_device, termination_z_device, vc_master_for, vc_position, vc_priority, vdcs, virtual_chassis, virtual_chassis_id, virtual_machines", "locations": [ { "line": 2, "column": 3 } ], "path": [ "device_list" ] } ] }` ### Expected Behavior As the filter `has_primary_ip` is no longer available (like cabled within InterfaceFilter) I tried the assigned-filter like I do for the connected as a replacement for cabled. I expected that this would work in the same way and provides a list of devices with a primary ip set. ### Observed Behavior Unfortunately the assigned-filter is applied to the device instead of the Interface and causes the error that it cannot be resolved to a field - as the assigned_object_id is on the interface, not the device.
adam added the type: bugstatus: acceptedtopic: GraphQLseverity: low labels 2025-12-29 21:48:19 +01:00
adam closed this issue 2025-12-29 21:48:19 +01:00
Author
Owner

@pheus commented on GitHub (Oct 8, 2025):

Hi! Thanks for the report and repro.

Root cause: the assigned filter on IPAddressFilter doesn’t use the prefix Strawberry‑Django passes for nested filters. When this filter runs under another filter (e.g., Device → primary_ip4), the unprefixed Q ends up targeting the outer model, so assigned_object_id can’t be resolved.

Fix:

@strawberry_django.filter_field()
def assigned(self, value: bool, prefix) -> Q:
-    return Q(assigned_object_id__isnull=(not value))
+    return Q(**{f"{prefix}assigned_object_id__isnull": not value})

After applying this, a nested query like the following works:

query {
  device_list(filters: { primary_ip4: { assigned: true } }) {
    id
    primary_ip4 { address }
  }
}

I can open a PR with this change. I also suggest a follow‑up issue to audit other @strawberry_django.filter_field methods that build raw Q(...) without applying prefix.

Thanks!

@pheus commented on GitHub (Oct 8, 2025): Hi! Thanks for the report and repro. **Root cause:** the `assigned` filter on `IPAddressFilter` doesn’t use the `prefix` Strawberry‑Django passes for nested filters. When this filter runs under another filter (e.g., `Device → primary_ip4`), the unprefixed `Q` ends up targeting the outer model, so `assigned_object_id` can’t be resolved. **Fix:** ```diff @strawberry_django.filter_field() def assigned(self, value: bool, prefix) -> Q: - return Q(assigned_object_id__isnull=(not value)) + return Q(**{f"{prefix}assigned_object_id__isnull": not value}) ``` After applying this, a nested query like the following works: ```graphql query { device_list(filters: { primary_ip4: { assigned: true } }) { id primary_ip4 { address } } } ``` I can open a PR with this change. I also suggest a follow‑up issue to audit other `@strawberry_django.filter_field` methods that build raw `Q(...)` without applying `prefix`. Thanks!
Author
Owner

@pheus commented on GitHub (Oct 9, 2025):

I’d like to contribute this fix. Could you please assign the issue to me? Thanks!

@pheus commented on GitHub (Oct 9, 2025): I’d like to contribute this fix. Could you please assign the issue to me? Thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11669