I’m trying to use the in operator to determine if a template variable on the current page is also a foreign key in another model.
The model is like so:
class WishlistItem(models.Model):
user = models.ForeignKey(User, related_name='wishlist_items')
issue = models.ForeignKey(Issue)
On the “Issue” page template, I’m trying to do this:
{% if issue in user.wishlist_items %}
{% else %}
{# A button to add the issue to the user's wishlist #}
{% endif %}
What I need to do is to get at the issue attribute of the wishlist_items QuerySet for the currently logged-in user and compare it to the issue object of the current page. I’m at a loss for how to do this. Do I need to write a templatetag to do this?
I ended up solving this by writing a template filter:
Then I could do something like this in a template:
I got the idea from the answer on this Stack Overflow question.