Fixes #20466: Correct handling of assigned filter logic #1099

Closed
opened 2026-04-05 20:39:47 +02:00 by MrUnknownDE · 0 comments
Owner

Originally created by @pheus on 10/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.
*Originally created by @pheus on 10/8/2025* ### Fixes: #20466 <!-- Please include a summary of the proposed changes below. --> #### 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 ```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}) ``` #### Why Nested GraphQL queries like the example below errored due to the missing prefix; with this change they work as intended. ```graphql 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`.
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/netbox#1099