You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "natcharam (via GitHub)" <gi...@apache.org> on 2025/07/28 20:43:30 UTC
[D] How to use Jinja template for "Inverse Selection" filter type [superset]
GitHub user natcharam created a discussion: How to use Jinja template for "Inverse Selection" filter type
I need help with Jinja templating for handling "Inverse Selection" filter type in SQL Lab/Dataset.
Setup:
I have 2 filters for the same column CAMPAIGN_GROUP:
Normal filter (Filter Type: "Filter") - works fine
Inverse Selection filter (Filter Type: "Inverse Selection") - need help with this
**What Works - Normal Filter:**
```
{% if filter_values('DEVICE_TYPE') %}
AND DEVICE_TYPE IN {{ filter_values('DEVICE_TYPE') | where_in }}
{% endif %}
{% if filter_values('CAMPAIGN_GROUP') %}
AND CAMPAIGN_GROUP IN {{ filter_values('CAMPAIGN_GROUP') | where_in }}
{% endif %}
```
**Problem:**
- How do I write the Jinja template to handle the "Inverse Selection" filter type?
- The inverse selection filter appears in the dashboard with the same column name CAMPAIGN_GROUP, but I need it to generate NOT IN logic instead of IN.
**Questions:**
- How can I distinguish between the normal filter and inverse selection filter in Jinja templates when they use the same column?
- Is there a different function to call for inverse selection filters?
- Does filter_values() work differently for inverse selection filter types?
- Should the inverse selection filter automatically generate NOT IN logic, or do I need to handle this manually?
**Expected Behavior:**
```
-- Normal filter should generate:
AND CAMPAIGN_GROUP IN ('value1', 'value2')
-- Inverse Selection filter should generate:
AND CAMPAIGN_GROUP NOT IN ('excluded1', 'excluded2')
```
**Additional Context:**
- Both filters target the same CAMPAIGN_GROUP column
- Users need both include and exclude functionality
GitHub link: https://github.com/apache/superset/discussions/34372
----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: notifications-unsubscribe@superset.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [D] How to use Jinja template for "Inverse Selection" filter type [superset]
Posted by "dosubot[bot] (via GitHub)" <gi...@apache.org>.
GitHub user dosubot[bot] added a comment to the discussion: How to use Jinja template for "Inverse Selection" filter type
<!-- Greeting -->
Hi @natcharam! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the superset team.
<!-- Answer -->
To handle both normal and "Inverse Selection" filters for the same column in a Jinja template, use the get_filters() macro instead of filter_values(). The get_filters() macro exposes the filter operator (IN or NOT IN), so you can generate the correct SQL logic for each filter type. Here’s how you can do it:
```jinja
{% for filter in get_filters('CAMPAIGN_GROUP') %}
{% if filter.op == 'IN' %}
AND CAMPAIGN_GROUP IN ({{ "'" + "', '".join(filter.val) + "'" }})
{% elif filter.op == 'NOT IN' %}
AND CAMPAIGN_GROUP NOT IN ({{ "'" + "', '".join(filter.val) + "'" }})
{% endif %}
{% endfor %}
```
This approach works because get_filters() returns a list of filters for the column, each with an op field (like 'IN' or 'NOT IN') and a val field (the values). You can loop through them and generate the appropriate SQL for each filter type.
filter_values() does not distinguish between filter types and will not handle NOT IN logic automatically—you need to use get_filters() for this use case. The NOT IN logic is not generated automatically; you must handle it in your template as shown above. See the code and docstring for get_filters() for more details and examples on this pattern
[here](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/jinja_context.py).
<!-- Dosu Comment Footer -->
*To reply, just mention [@dosu](https://go.dosu.dev/dosubot).*
---
<sup>How did I do? [Good](https://app.dosu.dev/response-feedback/5c6a2690-517c-4e8e-ba27-2f28269c08db?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/5c6a2690-517c-4e8e-ba27-2f28269c08db?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/5c6a2690-517c-4e8e-ba27-2f28269c08db?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/5c6a2690-517c-4e8e-ba27-2f28269c08db?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/5c6a2690-517c-4e8e-ba27-2f28269c08db?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/5c6a2690-517c-4e8e-ba27-2f28269c08db?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/5c6a2690-517c-4e8e-ba27-2f28269c08db?feedback_type=other)</sup> [](https://app.dosu.dev/a28d3c7e-a9d3-459e-9fb6-3a6f9ff4f357/ask?utm_source=github)&
nbsp;[](https://go.dosu.dev/discord-bot) [](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/superset/discussions/34372)
GitHub link: https://github.com/apache/superset/discussions/34372#discussioncomment-13914730
----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: notifications-unsubscribe@superset.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [D] How to use Jinja template for "Inverse Selection" filter type [superset]
Posted by "dosubot[bot] (via GitHub)" <gi...@apache.org>.
GitHub user dosubot[bot] edited a comment on the discussion: How to use Jinja template for "Inverse Selection" filter type
<!-- Greeting -->
Hi @natcharam! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the superset team.
<!-- Answer -->
To handle both normal and "Inverse Selection" filters for the same column in a Jinja template, use the get_filters() macro instead of filter_values(). The get_filters() macro exposes the filter operator (IN or NOT IN), so you can generate the correct SQL logic for each filter type. Here’s how you can do it:
```jinja
{% for filter in get_filters('CAMPAIGN_GROUP') %}
{% if filter.op == 'IN' %}
AND CAMPAIGN_GROUP IN ({{ "'" + "', '".join(filter.val) + "'" }})
{% elif filter.op == 'NOT IN' %}
AND CAMPAIGN_GROUP NOT IN ({{ "'" + "', '".join(filter.val) + "'" }})
{% endif %}
{% endfor %}
```
This approach works because get_filters() returns a list of filters for the column, each with an op field (like 'IN' or 'NOT IN') and a val field (the values). You can loop through them and generate the appropriate SQL for each filter type.
filter_values() does not distinguish between filter types and will not handle NOT IN logic automatically—you need to use get_filters() for this use case. The NOT IN logic is not generated automatically; you must handle it in your template as shown above. See the code and docstring for get_filters() for more details and examples on this pattern
[here](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/jinja_context.py).
<!-- Dosu Comment Footer -->
*To reply, just mention [@dosu](https://go.dosu.dev/dosubot?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=reply-with-mention&utm_term=apache%2Fsuperset).*
---
Docs are dead. Just use [Dosu](https://dosu.dev?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-tagline&utm_term=apache%2Fsuperset).
[](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-feedback&utm_term=apache%2Fsuperset&message_id=5c6a2690-517c-4e8e-ba27-2f28269c08db) [](https://github.dosu.com/apache/superset?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-ask-repo&utm_term=apache%2Fsuperset) [](https://app.dosu.dev/signup?referrer=openSource&source=github-footer&utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-share-team&utm_term=apache%2Fsuperset)
GitHub link: https://github.com/apache/superset/discussions/34372#discussioncomment-13914730
----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: notifications-unsubscribe@superset.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org