I have a queryset in Django:
puntos_jornalero1 = Equipo.objects.filter(
jlg1__puntos__etapa=2
).annotate(
puntos_jornalero1=Sum('jlg1__puntos__puntos', default=0)
)
If I make the queryset without the filter, I get 0 or the Sum correctly, but if I want to have the filter but in this case, when the result is 0, I get None. I need 0 because after that I need to Sum again the results.
My models, my query and my template are like these and everything works is I don`t use filter() in the subqueries, but when I use it I get None instead 0 if the lid1 or lid2 or jlg1 or..... has not puntos (points) in the Etapa (Stage) filtered.
Models.py:
class Equipo(models.Model):
alias=models.CharField(max_length=50, primary_key=True)
lid1=models.ForeignKey(Corredor,limit_choices_to={'lid':True, 'giro':True, 'tipo': "Rider"}, related_name="lid1", on_delete=models.CASCADE)
lid2=models.ForeignKey(Corredor,limit_choices_to={'lid':True, 'giro':True, 'tipo': "Rider"}, related_name="lid2", on_delete=models.CASCADE)
lid3=models.ForeignKey(Corredor,limit_choices_to={'lid':True, 'giro':True, 'tipo': "Rider"}, related_name="lid3", on_delete=models.CASCADE)
lid4=models.ForeignKey(Corredor,limit_choices_to={'lid':True, 'giro':True, 'tipo': "Rider"}, related_name="lid4", on_delete=models.CASCADE)
gre1=models.ForeignKey(Corredor,limit_choices_to={'gre':True, 'giro':True, 'tipo': "Rider"}, related_name="gre1", on_delete=models.CASCADE)
gre2=models.ForeignKey(Corredor,limit_choices_to={'gre':True, 'giro':True, 'tipo': "Rider"}, related_name="gre2", on_delete=models.CASCADE)
gre3=models.ForeignKey(Corredor,limit_choices_to={'gre':True, 'giro':True, 'tipo': "Rider"}, related_name="gre3", on_delete=models.CASCADE)
jlg1=models.ForeignKey(Corredor,limit_choices_to={'jor':True, 'giro':True, 'tipo': "Rider"}, related_name="jlg1", on_delete=models.CASCADE)
jlg2=models.ForeignKey(Corredor,limit_choices_to={'jor':True, 'giro':True, 'tipo': "Rider"}, related_name="jlg2", on_delete=models.CASCADE)
class Puntuacion (models.Model):
etapa=models.IntegerField()
dorsal=models.ForeignKey(Corredor, blank=True, null=True, related_name="puntos", on_delete=models.CASCADE)
categoria=models.CharField(max_length=50)
descripcion=models.CharField(max_length=150)
puntos=models.IntegerField()
class Corredor (models.Model):
dorsal=models.IntegerField(primary_key=True)
tipo=models.CharField(max_length=50)
nombre=models.CharField(max_length=50)
Views.py:
def clasificacion_etapa2(request):
puntos_lider1=Equipo.objects.filter(lid1__puntos__etapa=2).annotate(puntos_lider1=Coalesce(Sum('lid1__puntos__puntos'), Value(0)))
puntos_lider2=Equipo.objects.filter(lid2__puntos__etapa=2).annotate(puntos_lider2=Coalesce(Sum('lid2__puntos__puntos'), Value(0)))
puntos_lider3=Equipo.objects.filter(lid3__puntos__etapa=2).annotate(puntos_lider3=Coalesce(Sum('lid3__puntos__puntos'), Value(0)))
puntos_lider4=Equipo.objects.filter(lid4__puntos__etapa=2).annotate(puntos_lider4=Coalesce(Sum('lid4__puntos__puntos'), Value(0)))
puntos_gregario1=Equipo.objects.filter(gre1__puntos__etapa=2).annotate(puntos_gregario1=Coalesce(Sum('gre1__puntos__puntos'), Value(0)))
puntos_gregario2=Equipo.objects.filter(gre2__puntos__etapa=2).annotate(puntos_gregario2=Coalesce(Sum('gre2__puntos__puntos'), Value(0)))
puntos_gregario3=Equipo.objects.filter(gre3__puntos__etapa=2).annotate(puntos_gregario3=Coalesce(Sum('gre3__puntos__puntos'), Value(0)))
puntos_jornalero1=Equipo.objects.filter(jlg1__puntos__etapa=2).annotate(puntos_jornalero1=Coalesce(Sum('jlg1__puntos__puntos'), Value(0)))
puntos_jornalero2=Equipo.objects.filter(jlg2__puntos__etapa=2).annotate(puntos_jornalero2=Coalesce(Sum('jlg2__puntos__puntos'), Value(0)))```
equipos=Equipo.objects.annotate(puntos_lider1=Subquery(puntos_lider1.values('puntos_lider1'),output_field=IntegerField()),puntos_lider2=Subquery(puntos_lider2.values('puntos_lider2'),output_field=IntegerField()),puntos_lider3=Subquery(puntos_lider3.values('puntos_lider3'),output_field=IntegerField()),puntos_lider4=Subquery(puntos_lider4.values('puntos_lider4'),output_field=IntegerField()),puntos_gregario1=Subquery(puntos_gregario1.values('puntos_gregario1'),output_field=IntegerField()),puntos_gregario2=Subquery(puntos_gregario2.values('puntos_gregario2'),output_field=IntegerField()),puntos_gregario3=Subquery(puntos_gregario3.values('puntos_gregario3'),output_field=IntegerField()),puntos_jornalero1=Subquery(puntos_jornalero1.values('puntos_jornalero1'),output_field=IntegerField()),puntos_jornalero2=Subquery(puntos_jornalero2.values('puntos_jornalero2'),output_field=IntegerField())).annotate(puntos_equipo=F('puntos_lider1')+F('puntos_lider2')+F('puntos_lider3')+F('puntos_lider4')+F('puntos_gregario1')+F('puntos_gregario2')+F('puntos_gregario3')+F('puntos_jornalero1')+F('puntos_jornalero2')).order_by('-puntos_equipo')
return render(request,"PorraApp/clasificacion_etapa2.html",{"equipos":equipos})
Template file:
{% extends "PorraApp/base.html" %}
{% load static %}
{% block content %}
<h1 class="bg-success font-weight-bold text-dark py-4 px-5">CLASIFICACIÓN ETAPA 2</h1>
<section class="my-4">
<div class="container">
<ol class="bg-success font-weight-bold text-dark py-4 px-5">
{% for equipo in equipos %}
<li>
<h5 class="font-weight-bold"> {{equipo.alias}}</h5>
<p>{{equipo.puntos_equipo}} PUNTOS</p>
</li>
{% endfor %}
</ol>
</div>
</section>
{% endblock %}