I have a category table and a post table. For each category there are many posts. There is a foreign key relationship between these two tables so that each row in the post table also has a column for associate category id. Now, I’m querying category table and sending it to the jinja2 template. In the template, I am looping through each category and displaying all posts associated with it. Here is the code:
{% for val in cat_list %}
{% for i in val.posts.all() %}
<p>{{ i.msg }}</p>
{% endfor %}
{% endfor %}
this gives me category wise list of all posts.
Now, I do not want to display posts that has been deleted i.e. their status is Deleted. So I’m trying the following:
{% for val in cat_list %}
{% for i in val.posts.filter(val.posts.status != 'Deleted' ).all() %}
<p>{{ i.msg }}</p>
{% endfor %}
{% endfor %}
But it is not filtering out the deleted posts and is still giving me all posts, deleted and non-deleted.
What am I missing?
I tried using .filter()
to filter out ‘Deleted’ posts but it seems my query formation is incorrect.