What is the Jinja rejectattr Filter?
nThe Jinja rejectattr filter is a powerful tool for filtering a sequence of objects based on an attribute. It’s essentially the inverse of the selectattr filter. While selectattr keeps items that pass a test, rejectattr removes them, keeping only the items that fail the test. This filter is perfect for scenarios where you want to exclude a specific subset of data, such as hiding inactive users or unpublished posts. By using rejectattr, you can write clean, declarative, and highly readable template code without needing to create complex filtering logic in your backend.nn
nn
How the Filter Works
nThe rejectattr filter operates on an iterable of objects (like a list of dictionaries or Python objects) and returns a new iterable containing only the objects whose specified attribute fails a given test.nnThe basic syntax is simple:n
{{ my_objects | rejectattr("my_attribute") }}
nWhen no specific test is provided, the filter evaluates the attribute’s value as a boolean. It will reject any object where the attribute’s value is “truthy” (e.g., True, a non-empty string, a non-zero number) and keep those that are “falsy” (False, None, an empty string, or zero). This is its most common use case.nnLike many Jinja filters, rejectattr returns an iterator for efficiency. This is particularly beneficial for large datasets, as it processes items one by one rather than creating a whole new list in memory. You will typically use this filter within a for loop to render the filtered content.nn
nn
Key Parameters and Options
nThe rejectattr filter’s behavior can be customized with two main parameters:n
- n
value(iterable): The sequence of objects that you want to filter.attribute(string): The name of the attribute on each object to be tested. This can be a simple key name or a dot-separated path for nested attributes (e.g.,"user.profile.is_hidden").test(string, optional): The name of a test to apply to the attribute’s value. Jinja has many built-in tests that you can use, such as:n- n
"none": Rejects objects where the attribute’s value isNone."equalto"or"eq": Rejects objects where the attribute value is equal to a given value."in": Rejects objects where the attribute’s value is present in a list.
n
n
n
n
test_arguments(optional): The arguments required by the specified test.
n
n
n
n
nn
nn
Practical Examples
nLet’s walk through some examples to illustrate how rejectattr can simplify your templates.n
Example 1: Hiding Inactive Users
nThis is a classic use case. You have a list of users, and you only want to display the ones who are not active.nnJinja Template:n
{% set users = [n {'name': 'Alice', 'is_active': True},n {'name': 'Bob', 'is_active': False},n {'name': 'Charlie', 'is_active': True}n] %}n<h3>Active Users</h3>n<ul>n{% for user in users | rejectattr("is_active") %}n <li>{{ user.name }}</li>n{% endfor %}n</ul>
nRendered HTML:n
<h3>Active Users</h3>n<ul>n <li>Bob</li>n</ul>
nThe filter rejects Alice and Charlie because their is_active attribute is True, leaving only Bob.n
Example 2: Rejecting Posts with No Author
nHere, we use a specific test to filter out items where a certain attribute has no value.nnJinja Template:n
{% set posts = [n {'title': 'First Post', 'author': 'Alice'},n {'title': 'Draft Post', 'author': None},n {'title': 'Second Post', 'author': 'Bob'}n] %}n<h3>Posts with an Author</h3>n<ul>n{% for post in posts | rejectattr("author", "none") %}n <li>{{ post.title }}</li>n{% endfor %}n</ul>
nRendered HTML:n
<h3>Posts with an Author</h3>n<ul>n <li>First Post</li>n <li>Second Post</li>n</ul>
nThe rejectattr filter, along with the "none" test, effectively removes the “Draft Post” from the list.n
Example 3: Rejecting Items Equal to a Specific Value
nThis example demonstrates how to exclude objects based on a specific value.nnJinja Template:n
{% set products = [n {'name': 'T-Shirt', 'category': 'Clothing'},n {'name': 'Coffee Mug', 'category': 'Home Goods'},n {'name': 'Jeans', 'category': 'Clothing'}n] %}n<h3>Other Items</h3>n<ul>n{% for product in products | rejectattr("category", "equalto", "Clothing") %}n <li>{{ product.name }}</li>n{% endfor %}n</ul>
nRendered HTML:n
<h3>Other Items</h3>n<ul>n <li>Coffee Mug</li>n</ul>
nThe filter successfully removes all products in the “Clothing” category, leaving only the “Coffee Mug.”nn
nn
Conclusion
nThe Jinja rejectattr filter is a versatile and elegant solution for filtering data in your templates. By providing a clear and efficient way to exclude objects based on their attribute values, it helps you write cleaner code and focus on the data you want to display. By understanding how to use rejectattr with its boolean default or with specific tests, you can dramatically improve the readability and maintainability of your Jinja templates.nn
