0

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

0

2 Answers 2

1

Why not something like?

Todo.objects.filter(
    state = <state>,
    project__srv = <srv_instance>
)

Update:

template.html:

{% for srv in srvs %}
  <div> srv year: {{srv.srv_year}} </div>
  {% for project in srv.project_set.all %}
    <div> project id: {{project.id}} </div>
    {% for todo in project.todo_set.all %}
      {% if todo.state %}
        <div> todo id: {{todo.id}} </div>
      {% endif %}
    {% endfor %}
  {% endfor %}
{% endfor %}
Sign up to request clarification or add additional context in comments.

2 Comments

sorry, but i don't understand how to incorporate this into code. I understand that I must get the tasks per project and send it as context for each project listed
We'll you don't really need to do this in the view - see my updated answer.
1

Try the below code:

in your view:

projects = Project.objects.filter(todo__state=True)

in your template:

<table>
    <tr>
        <th>srv year</th>
        <th>project ID</th>
        <th>Total number of completed tasks</th>
    </tr>
    {% for project in projects %}
    <tr>
        <td>{{ project.srv.srv_year }}</td>
        <td>{{ project.pk }}</td>
        <td>{{ project.todo_set.count }}</td>
    </tr>
    {% enfor %}
</table>

3 Comments

How can I send this value to each project in the template? the Django system on the cli correctly detects the number of completed tasks per project associated with the SRV
@Drennos, i have updated my answer. This should be work.
I managed to send extra content to my template by adding definitions in my model

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.