user2959896
user2959896

Reputation:

How to loop over two querysets at the same time?

I'm trying to display a list of all comments posted by a user on all stories they have commented on. Currently i have this:

{% for story in all_user_stories %}
{{ story.title }}
{{ story.comments.all }}
{% endfor %}

There is a story and a comment database table where the comments field is a foreign key in the comment table with a related name "comments". The problem is that the "story.comments.all" line just received all comments for all stories the user has commented on instead of just the comments a user made on a specific story. I have looked through the docs and i cant find a suitable or a good way of doing this.

Does someone know how i can do this in a nice clean way, or if at all ? Do i have do create another query set from the comment table, and in that case, how would i iterate of the two at once. Ive seen a couple of ways people do this but then the querysets has the same number which isnt the case here.

Update:

This is the story and comment model:

class Story(models.Model):
    user = models.ForeignKey(User)
    date_added = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)
    title = models.CharField(max_length=150)
    description = models.CharField(blank=True, null=True, max_length=2000)
    story_text = models.TextField()
    category = models.ForeignKey(Category, blank=True, null=True)

class Comment(models.Model):
    user = models.ForeignKey(User)
    date_added = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)
    emailOnReply = models.NullBooleanField(blank=True, null=True)
    comment_text = models.TextField()
    story = models.ForeignKey(Story, related_name="comments")

    def __unicode__(self):
        return self.comment_text

The comments field im refering to in the code is the related_name argument in the story model field in the Comment table, this returns the comments but it returns all of them instead of just the comment related to the story for loop is going through. The user model is the user model from djangos authentication system.

This is the relevant html code:

<table class="table table-hover">
    <thead>
      <tr>
        <th>Title</th>
        <th>Date added</th>
        <th>Last modified</th>
        <th>Publicity</th>
        <th>Comments</th>
        <th>Votes</th>
        <th>Views</th>
      </tr>
    </thead>
    <tbody>
      <tr>
  {% for story in all_user_story %}
        <td><a href="/story/display/{{ story.id }}"> {{ story.title }}</td>
        <td>{{ story.date_added }}</td>
        <td>{{ story.date_modified }}</td>
        <td>{{ story.story_access_level }}</td>
        <td>
          {{ story.comments.all }} <----- How to only display the comment(s) related to this specific story the for loop is looping through
        </td>
        <td><img src="{% static 'images/arrow_up.png' %}"> {{ story.votes }} <img src="{% static 'images/arrow_down.png' %}"></td>
        <td>
          {% if story.views %}
            {{ story.views }} 
          {% else %}
            0 {% endif %}
        </td>
        <td> <a href="/user/{{ user }}/story/{{ story.id }}/edit">Edit story</a></td>
      </tr>
  {% endfor %}
    </tbody>
  </table

Upvotes: 0

Views: 671

Answers (1)

HAL
HAL

Reputation: 2061

You can look up the related comments for a story by using the "related manager" (more info about it here in the Django documentation).

The code would look like this:

{% for story in all_user_stories %}
  {{ story.title }}
  {% for comment in story.comment_set.all %}
    {{ comment.comment_text }}
  {% endfor %}
{% endfor %}

It should also be possible to use the "related name" story.commments instead of story.comment_set when looking up the comments for a specific story:

{% for story in all_user_stories %}
  {{ story.title }}
  {% for comment in story.comments.all %}
    {{ comment.comment_text }}
  {% endfor %}
{% endfor %}

Upvotes: 1

Related Questions