Reputation: 554
I am looking for a solution to show a Models data elements along with this annotated data, e.g. I have a TestRun model which has M2M relationship with TestCases, so on the UI page for testruns list page want to show TsetRun data along with the count of testcases grouped by its status.
e.g. TestRun.ID = 1, TestRun.Name = Regression
Total TCs in TR = 4
Passed TC = 2 | Blocked TC = 1 | Failed TC = 1
along with a table for each run record to show the above 4 testcases like
TC_ID|TC_NAME|TC_STATUS|
1 |TC_101 | Passed |
4 |TC_102 | Passed |
5 |TC_105 | Failed |
7 |TC_107 | Blocked |
I have following models
class TestRun(models.Model):
testcases = models.ManyToManyField(TestCase, through="TestRunCases")
project = models.ForeignKey(Project, related_name="project_testruns", on_delete=models.PROTECT)
name = models.CharField(max_length=255)
class TestRunCases(models.Model):
testcases = models.ForeignKey(TestCase, on_delete=models.CASCADE)
testrun = models.ForeignKey(TestRun, on_delete=models.CASCADE)
testcase_status = models.ForeignKey(TestCaseStatus, null=True, default=1, on_delete=models.PROTECT)
I want to show the count of test cases for each run with status, e.g. For TestRun.id=1, Total TC#=3,Passed=2, Failed=1, template eg. provided below
I tried using following in my views, using CBV
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["testcases"] = (
TestRunCases.objects.filter(testrun_id__in=testrun_qs)
.values("testrun_id", "testcase_status__status_value")
.annotate(status_count=Count("id"))
)
return context
And in my template
{% for testrun in object_list %}
<div>
{{ testrun.id }}-{{ testrun.name }}
<div>
{{ testrun.testcases.all.count }} Test Case{{ testrun.testcases.all.count|pluralize }}
{% regroup testcases by testrun_id as tc_by_run %}
{% for testrun_id in tc_by_run %}
{% if testrun_id.grouper == testrun.id %}
{% for tc_count_by_sts in testrun_id.list %}
{{ tc_count_by_sts.status_count }} {{ tc_count_by_sts.testcase_status__status_value }}
{% endfor %}
{% endif %}
{% endfor %}
</div>
<table>
<thead>
<tr>
<th>Test Case Name</th>
<th>Test Case Status</th>
</tr>
</thead>
<tbody>
{% for tr_item in testrun.testruncases_set.all %}
<tr>
<td>{{ tr_item.testcases.tcname }}</td>
<td>{{ tr_item.testcase_status.status_value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endfor %}
Is this the recommended way or this can be implemented using another approach.
Upvotes: 0
Views: 21