Filtering records by related fields #301
-
Is it possible to filter records by related field. For example
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Obviously not :-) I'll investigate whether that is a bug or a (Django/NetBox) 'feature'. |
Beta Was this translation helpful? Give feedback.
-
OK, done. It's a 'feature'. In the current implementation of filtersets filter expressions apparently filters can't be nested, so in order to do fully orthogonal nested filtering all filters related to Example: You can filter for zone_id = django_filters.ModelMultipleChoiceFilter(
queryset=Zone.objects.all(),
label="Parent Zone ID",
)
zone = django_filters.ModelMultipleChoiceFilter(
queryset=Zone.objects.all(),
field_name="zone__name",
to_field_name="name",
label="Parent Zone",
)
view_id = django_filters.ModelMultipleChoiceFilter(
queryset=View.objects.all(),
field_name="zone__view",
label="ID of the View the Parent Zone belongs to",
)
view = django_filters.ModelMultipleChoiceFilter(
queryset=View.objects.all(),
field_name="zone__view__name",
to_field_name="name",
label="View the Parent Zone belongs to",
) Note that As a little proof of concept I threw in the required filter: zone_status = django_filters.ModelMultipleChoiceFilter(
queryset=Zone.objects.all(),
field_name="zone__status",
to_field_name="status",
label="Parent Zone Status",
) And after restarting NetBox all of a sudden this query works:
The bad news is that I'm afraid you'll have to filter the resulting JSON in order to achieve your goal ( |
Beta Was this translation helpful? Give feedback.
-
Awesome, thank you for detailed explanation. Just tried GraphQL, same result works:
does not work:
I guess jq will be best solution for now. |
Beta Was this translation helpful? Give feedback.
OK, done. It's a 'feature'.
In the current implementation of filtersets filter expressions apparently filters can't be nested, so in order to do fully orthogonal nested filtering all filters related to
Zone
need to be redefined in the filterset forRecord
in order to be usable in queries. This does not really scale.Example: You can filter for
zone
(name),zone_id
,view
(name) andview_id
because the following filters are defined: