I have the following models in my project. An SRV contains many projects, and each project contains several tasks. I am detailing each SRV in a template and showing a list of all associated projects.
class Srv(models.Model):
srv_year = models.CharField(max_length=4)
class Project(models.Model):
srv = models.ForeignKey(Srv, on_delete=models.CASCADE, null=True, blank=True)
class Todo(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE, null=True, blank=True)
state = models.BooleanField(blank=True, null=True, default=False)
in views.py I want to get the completed tasks associated with each project, but I always get all the tasks associated with all projects from the SRV (main model)
class srvdetail(LoginRequiredMixin, DetailView):
model = Srv
template_name = 'srv_detail.html'
slug_field = 'slug'
UPDATE:
Add a couple of definitions in the Project model and printed them in the template as follows:
class Project(models.Model):
#...
def todo_done(self):
return self.todo_set.filter(state=True).count() * 100 / self.todo_set.all().count()
def todo_left(self):
return self.todo_set.filter(state=False).count() * 100 / self.todo_set.all().count()
In my template:
{% for project in srv.project_set.all %}
{{project.todo_done|floatformat:0|intcomma}}% complete
{{project.todo_left|floatformat:0|intcomma}}% left
{% endfor %}
Thank you for your comments @NKSM