0

I have a django template that has two different querysets passed through to the html template. I want to go through each item in an query set and display the content, I then want to go through the second array while still in the iniital loop and check to see if two peices of info match. If there is a match between the two peices of info, I want to just grab the first result from teh matching info.

I have a query that grabs a list of group items. I have another query that grabs the list of activities. I want to get all the activity objects that are related to the group through the foreignkey and reference to the group within the activity object. From the list of related activities ot the group, I want to grap the first record and display that record only... If a group (a) has 10 activities related tot he group, I want to grab the first one and display it... Here is my code:

html tempalte:

    <div class="col-12 info-div">
      {% for group in groups %}
        <div class="row">
          <p class="col-12">
            <a href="{% url 'group_home' group.group.name|slugify group.group.id %}">{{ group.group.name }}</a>
          </p>
          {% for act in activities %}
            {% if act.group.id == group.id %}
            <p class="col-12">
              {{  act.description | first  }}
            </p>
            {% endif %}
          {% endfor %}
        </div>
      {% endfor %}
    </div>

the html above just gives me the first letter of each related activity compared to what i want which is the first item where the group in the activity matches the group being looped through

views.py file :

# grab all of the groups you are a member of
groups = Member.objects.filter(user = user).all()
# the following will be the last activity created for each group
activities = GroupActivity.objects.all()

models.py

class Group(models.Model):
    name = models.CharField(max_length = 25)
    description = models.CharField(max_length = 250, null=True)
    created_by = models.ForeignKey(User, default=1, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True)
class GroupActivity(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    group = models.ForeignKey(Group, on_delete=models.CASCADE)
    expense = models.ForeignKey(Expense, null=True, on_delete=models.CASCADE)
    bundle = models.ForeignKey(Bundle, on_delete=models.CASCADE, null=True)
    description = models.CharField(max_length=200, default='some action')
    host = models.CharField(max_length=100, null=True)
    reference = models.IntegerField(default=0)
    category = models.SmallIntegerField(default = 1)
    # 1 - general
    # 2 - specific
    # 3 - no validation
    # 4 - validation
    created = models.DateTimeField(auto_now_add=True)
1
  • Do you have to use the template? This can be compared in the view with more built in python functionality Commented Dec 5, 2017 at 1:45

1 Answer 1

1

Better answer:

{% for group in groups %}
    <div class="row">
      <p class="col-12">
        <a href="{% url 'group_home' group.group.name|slugify group.group.id %}">{{ group.group.name }}</a>
      </p>
      {% with group.groupactivity_set.all|first as act %}
        {% if act %}
        <p class="col-12">
          {{  act.description }}
        </p>
        {% endif %}
      {% endwith %}
    </div>
  {% endfor %}

Here we are doing everything in the template, as simple as that.

Sign up to request clarification or add additional context in comments.

5 Comments

will this return a single object or a list of objects. because if i have 5 groups i want 5 groups and 5 first acitivty items.
It returns a list of activity objects, the first one of each group. If you want the group object also, please include it in the list comprehension
Updated the answer.
It worked I had to use the following with statement {% with activities.all|first as act %}... then it worked thank you.
Glad it helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.