How can I go through the reverse general relation in a Django template?

I have the following class that I use to bookmark items:

class BookmarkedItem(models.Model):
     is_bookmarked = models.BooleanField(default=False)
     user = models.ForeignKey(User)
     content_type = models.ForeignKey(ContentType)
     object_id = models.PositiveIntegerField()
     content_object = generic.GenericForeignKey()

And I define the inverse general relation as follows:

class Link(models.Model):
    url = models.URLField()
    bookmarks = generic.GenericRelation(BookmarkedItem)

In one of my views, I generate a set of requests for all links and add it to the context:

links = Link.objects.all()
context = {
     'links': links
}
return render_to_response('links.html', context)

The problem I am facing is how to cross common relationships in my template. For each link, I want to check the is_bookmarked attribute and change the add / remove bookmark button according to whether the user already has bookmarks or not. Can this be done in a template? Or do I need to do some additional filtering in the view and pass another request?

+5
1

GenericRelation "bookmarks", :

{% for bookmark in link.bookmarks.all %}
+4

All Articles