Reputation: 824
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)
Upvotes: 0
Views: 2555
Reputation: 1793
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.
Upvotes: 1