2

I want to execute this MySQL query in Django:

SELECT Descripcion,count(*)
FROM votaciones.Tarjeton 
WHERE Voto_idVoto=1 
GROUP BY Descripcion;

Expected result from MySQL:

'', '1'

'Camilo Gomez Ortega', '3'

'JUan Jose Marquez', '3'

'Pedro Pablo de la Mar', '15'

'Sandra Mejia Gutierez', '4'

My model is:

class Tarjeton(models.Model):
idtarjeton = models.AutoField(db_column='idTarjeton', primary_key=True)  # Field name made lowercase.
descripcion = models.CharField(db_column='Descripcion', max_length=45)  # Field name made lowercase.
cadidatos_idcadidatos = models.ForeignKey(Cadidatos, models.DO_NOTHING, db_column='Cadidatos_idCadidatos')  # Field name made lowercase.
voto_candidato = models.ForeignKey('Voto', models.DO_NOTHING, db_column='Voto_idVoto')  # Field name made lowercase.

class Meta:
    managed = False
    db_table = 'Tarjeton'
1

1 Answer 1

2

Try this way:

from django.db.models import Count
Tarjeton.objects.filter(voto_candidato=1).values('descripcion').annotate(Count('descripcion')).order_by()
Sign up to request clarification or add additional context in comments.

4 Comments

Observacion Votos Pedro Pablo de la Mar 15 Sandra Mejia Gutierez 4 JUan Jose Marquez 3 Camilo Gomez Ortega 3 1
the result is not correct?
the result is same as the one in your question, just it is ordered.
yes, result is correct, thank

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.