Skip to content

Conversation

pheus
Copy link
Contributor

@pheus pheus commented Oct 8, 2025

Fixes: #20466

Summary

  • Correct the custom GraphQL filter IPAddressFilter.assigned to use the nested relation prefix supplied by Strawberry‑Django.
  • Prior behavior built a non‑prefixed Q, so when used under another filter (e.g., Device → primary_ip4), Django attempted to resolve assigned_object_id on the outer model and failed.
  • New behavior dynamically qualifies the lookup with prefix, ensuring the condition targets the nested IPAddress.

Change

@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})

Why

Nested GraphQL queries like the example below errored due to the missing prefix; with this change they work as intended.

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

Follow‑up

  • Propose a small audit for other @strawberry_django.filter_field methods that construct raw Q(...) lookups without applying prefix.

Updates `assigned` filter to dynamically generate keys using the
`prefix` parameter. This addresses potential issues with prefixed
filters and ensures accurate query construction.

Fixes netbox-community#20466
@jnovinger jnovinger requested review from a team and bctiemann and removed request for a team October 9, 2025 06:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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