I want to get to know the number 'tasks' (Pendentes) for each 'Customer' (Cliente)
I have this models in my models.py:
class Cliente(models.Model):
id = models.IntegerField(primary_key=True)
nome = models.CharField(max_length=200, help_text="Nome")
localidade = models.CharField(max_length=100, help_text="Localidade")
[...]
class Pendente(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, help_text="ID Unico")
cliente = models.ForeignKey('Cliente', on_delete=models.RESTRICT, blank=True, null=True)
memo = models.TextField(null=True, blank=True, help_text="Memória Descritiva", max_length=200)
criado = models.DateField(default=date.today, null=True, blank=True, help_text="Data registo ")
concluir = models.DateField(null=True, blank=True, help_text="Data de execução máxima ")
tecnico = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
tecnicoatribuido = models.ForeignKey('TecnicoAtribuido', on_delete=models.SET_NULL, blank=True, null=True)
Basicly i want to run this sql:
"select A.id, A.nome, A.localidade, count(B.id) as tarefas
from todolist_pendente B
LEFT JOIN todolist_cliente A
ON A.id = B.cliente_id
GROUP by A.id;"
Can't seem to figure out how it would work on a view.
I'm able to get the count per cliente with :
resultado = Pendente.objects.values('cliente').order_by('cliente').annotate(count=Count('id'))
But this doesn't render the name of each customer on the template.
On my template I'm using :
<ul>
{% for obj in resultado %}
<li>{{ obj.cliente}} : {{ obj.count}}</li>
{% endfor %}
</ul>
This is rendering:
6 : 2
35 : 1
40 : 1
92 : 1
106 : 1
105 : 1
is there a way to get the foreignkey value 'nome' so that it could be rendered like this:
id Nome Localidade Tarefas
6 Cliente A Quarteira 2
35 Cliente B Lisboa 1
40 Cliente C Barreiro 1
92 Cliente D Lisboa 1
105 Cliente E Faro 1
106 Cliente F Barreiro 1
Help would be appreciated...